How to determine the player's altitude

Discuss modding questions and implementation details.
Post Reply
BadLuckBurt
Posts: 533
Joined: Sun Nov 05, 2017 8:30 pm

How to determine the player's altitude

Post by BadLuckBurt » Sun May 10, 2020 10:06 am

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() + "%");
Last edited by BadLuckBurt on Sun May 10, 2020 1:36 pm, edited 1 time in total.
Daggerfall Unity on UESP: https://en.uesp.net/w/index.php?title=T ... fall_Unity
Daggerfall Unity on Nexus Mods: https://www.nexusmods.com/daggerfallunity
My github repositories with mostly DFU related stuff: https://github.com/BadLuckBurt

User avatar
Interkarma
Posts: 5473
Joined: Sun Mar 22, 2015 1:51 am

Re: How to determine the player's altitude

Post by Interkarma » Sun May 10, 2020 11:22 am

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.

BadLuckBurt
Posts: 533
Joined: Sun Nov 05, 2017 8:30 pm

Re: How to determine the player's altitude

Post by BadLuckBurt » Sun May 10, 2020 11:47 am

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.
Daggerfall Unity on UESP: https://en.uesp.net/w/index.php?title=T ... fall_Unity
Daggerfall Unity on Nexus Mods: https://www.nexusmods.com/daggerfallunity
My github repositories with mostly DFU related stuff: https://github.com/BadLuckBurt

User avatar
Interkarma
Posts: 5473
Joined: Sun Mar 22, 2015 1:51 am

Re: How to determine the player's altitude

Post by Interkarma » 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. :D

BadLuckBurt
Posts: 533
Joined: Sun Nov 05, 2017 8:30 pm

Re: How to determine the player's altitude

Post by BadLuckBurt » Sun May 10, 2020 12:28 pm

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. :D
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
Daggerfall Unity on UESP: https://en.uesp.net/w/index.php?title=T ... fall_Unity
Daggerfall Unity on Nexus Mods: https://www.nexusmods.com/daggerfallunity
My github repositories with mostly DFU related stuff: https://github.com/BadLuckBurt

BadLuckBurt
Posts: 533
Joined: Sun Nov 05, 2017 8:30 pm

Re: How to determine the player's altitude

Post by BadLuckBurt » Sun May 10, 2020 2:04 pm

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%.
Daggerfall Unity on UESP: https://en.uesp.net/w/index.php?title=T ... fall_Unity
Daggerfall Unity on Nexus Mods: https://www.nexusmods.com/daggerfallunity
My github repositories with mostly DFU related stuff: https://github.com/BadLuckBurt

Post Reply