- Home /
Function to rotate my Player smoothly taking the y and use it to have angle
I need a function that works with my position.y of Player if y = -3.25 the function will return angle for example take 45 degree on y = -3.25 it will give 45 degree on the max y here 9.75 it will give 0 and if falling on y = -3.25 it will give -45 degree like on this picture
I want to make my Player rotate with value that function gives already I have something like that
float rot = returnRotation(transform.position.y); // taking the y
if ((isFalling && rot < 0) || (isFalling == false && rot > 0)) // if falling the value = value * -1
rot *= -1;
currentEulerAngles += new Vector3(0, 0, (rot - lastRotate) * 70) * Time.deltaTime; // some maths
lastRotate = rot;
transform.localEulerAngles = currentEulerAngles; // something that should make player rotate
private float returnRotation(float y)
{
float rot = rotationAngle - (y + 3.25f) * (rotationAngle / (maxHeight + 3.25f)); /// rotationAngle = 45 degree and the maxHeight is the max y that player can reach on my image this equals 9.75
return rot;
}
but this rotatation is not very smooth and it looks just bad and I have no idea how I can change it I even do not fully undestrand this "maths" I would be very glad if somebody could help me . I'm very sorry for my english.
Answer by Skiles43 · Nov 09, 2020 at 12:56 PM
You are making the transform rotate based on the time from the start up of the program. You need to make it rotate based on time since the last frame. Change the last argument passed to RotateTowards() from Time.time to Time.deltaTime:
transform.rotation = Quaternion.RotateTowards(transform.rotation,Quaternion.Euler(0, 0, angle),Time.deltaTime);
I don't know what do u mean sory I'm new to program$$anonymous$$g I have something like that float rot = returnRotation(transform.position.y); // taking the y if ((isFalling && rot < 0) || (isFalling == false && rot > 0)) // if falling the value = value -1 rot = -1; currentEulerAngles += new Vector3(0, 0, (rot - lastRotate) 70) Time.deltaTime; // some maths lastRotate = rot; transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0, 0, rot), Time.deltaTime);
btw I haven't added my return function now u can see it sory my bad