Page 1 of 1
How to determine the player's altitude
Posted: Sun May 10, 2020 10:06 am
by BadLuckBurt
Hi,
I'm currently trying to determine the player's altitude. I think I've found the correct way of doing it but would like to ask if anyone can confirm it is:
- edit
Updated the code example to display altitude percentage
Code: Select all
GameObject player = GameManager.Instance.PlayerObject;
float altitude = player.transform.position.y;
altitude += Mathf.Abs(GameManager.Instance.StreamingWorld.WorldCompensation.y);
float altitudePercentage = (altitude / DaggerfallUnity.Instance.TerrainSampler.MaxTerrainHeight) * 100;
DaggerfallUI.AddHUDText("Player altitude: " + altitudePercentage.ToString() + "%");
Re: How to determine the player's altitude
Posted: Sun May 10, 2020 11:22 am
by Interkarma
That looks OK to get absolute altitude above the virtual "zero" height.
Note this can change based on mods using terrain samplers. Be careful how you use this value if you're going to store it anywhere for later.
Re: How to determine the player's altitude
Posted: Sun May 10, 2020 11:47 am
by BadLuckBurt
Thanks! Good to know I am on the right track.
I assume the resulting height value that I get is relative to the max terrain height defined in the sampler (1539f in the default one), if so I could calculate a height percentage instead.
Re: How to determine the player's altitude
Posted: Sun May 10, 2020 11:57 am
by Interkarma
That sounds right. Player can get much higher than max terrain elevation though (levitate or set_jump 100), but I guess then you'd see 1000% altitude or something when they blast off into the stratosphere.
Re: How to determine the player's altitude
Posted: Sun May 10, 2020 12:28 pm
by BadLuckBurt
Interkarma wrote: ↑Sun May 10, 2020 11:57 am
That sounds right. Player can get much higher than max terrain elevation though (levitate or set_jump 100), but I guess then you'd see 1000% altitude or something when they blast off into the stratosphere.
Thanks again
That is fine actually. Ralzar plans to make altitude a thing in his Climate mod eventually and I was curious to see how to get the correct height values. Scaling it to the percentage of max height of a sampler should allow it to work for all samplers, will do some more testing
Re: How to determine the player's altitude
Posted: Sun May 10, 2020 2:04 pm
by BadLuckBurt
I tested this with the default sampler, my own sampler and Mountains and Hills. I think the calculation is reliable enough for people to do altitude stuff based on the result.
Moving to the higher altitudes in the Dragontail Mountains with the default sampler and M&H yields values in 80-90% range although the Ephesus region altitude exceeds 100% sometimes, same goes for the big mountain in Tigonus. My own sampler plays a bit nicer with those areas because I increased the max height to 2560f there.
I placed the demo code on Github in case anyone wants to check this out:
https://github.com/BadLuckBurt/blb-alti ... es/tag/1.0
It's not all that useful, except for ensuring that no terrain elevations exceed 100%.