- Home /
Move UI based on player movement
Hello guys.
I hope someone can help me.
I have my player rotate from 0 to 180, and i want to move mu UI to allow it to stay in a certain position near player head.. i thought to clamp player rotation in a range of 0-1 then move my UI from -5 to 5 based on the player position, -5 for bottom position and 5 top one... But i can’t figure out to how clamp it.. Anyone can help me?
setting up your canvas render mode to world space. and updating its transform as other objects
I already did it, i want to know just how to clamp player rotation into a range of 0 1
Thank’s you anyway
clamp player rotation? you mean that when player rotation is 360 make it 1? and when is 180 0.5 for example?
float yourDesiredRotation = player.transform.rotation.eulerAngles.x / 360;
change the x for y or z if you want the other angles.
Thank’s you! Sorry for my question but for a $$anonymous$$us is working in the same way?
no, try tihs code
float angle = player.transform.rotation.eulerAngles.x;
float desiredValue = 0;
if(angle >= 0)
{
desiredValue = angle / 360;
}
else
{
desiredValue = (360.0f + angle) / 360;
}
Why don't you parent your UI to the player's head so it will automatically follow and remain on the same position relative to it.
befause i already did it and it's not following player head as expected..
Answer by Sylon87 · Feb 08, 2019 at 02:28 AM
if(_playerAngle[id] >= 0)
{
_target[id] = _playerAngle[id] / 360;
float FixedY = Mathf.Lerp(12f,13f,_target[id]);
Vector3 fixedPos = new Vector3(_iconObject[id].transform.position.x,FixedY,_iconObject[id].transform.position.z);
_iconObject[id].transform.position = fixedPos;
}
else
{
_target[id] = (360 + _playerAngle[id]) / 360;
float FixedY = Mathf.Lerp(12f,13f,_target[id]);
Vector3 fixedPos = new Vector3(_iconObject[id].transform.position.x,FixedY,_iconObject[id].transform.position.z);
_iconObject[id].transform.position = fixedPos;
}
this is what i have
my mistake, it was just because i has to lerp from 13 to 12 on the negative one..., well now is working fine until it go on a full 360 rotation in that case it will go out of that range...i'm missing something?
the problem is on value that will return a value bigger than -180, so it will get wrong... there is a way to fix it to return a value on the -180 and 180 range?