- Home /
Player movement in the direction of rotation INACCURATE?
So I have just wrote a code that moves the player in the direction of his "face"/rotation but for some reason it works really inaccurately. For example when I am, the player is facing a 45 degrees angle it moves in 35-40 degrees angle. Can someone help me fix it? Thanks ^^
void Update()
{
Angle = Rigid.rotation.y;
if (Input.GetButton("Vertical"))
{
Rigid.MovePosition(Rigid.position + new Vector3(0.02f * Input.GetAxis("Vertical")*Mathf.Sin(Angle*Mathf.PI), 0 , 0.02f * Input.GetAxis("Vertical") * Mathf.Cos(Mathf.PI * Angle)));
}
}
Answer by andrew-lukasik · Apr 28, 2021 at 06:05 PM
Angle = Rigid.rotation.y;
Rigidbody.rotation.y
is not an angle, partner. It's an imaginary component of a quaternion.
Answer by The-Peaceful · Apr 28, 2021 at 06:35 PM
As @andrew-lukasik said, rotation.y does not return an angle, it returns the Y component of a Quaternion and they don't really work as angles. If you do actually want to get the angle that the Player has been rotated towards you can use the EulerAngles, which actually return angles. Also, though I don't know if you might have considered this, you can get the forward, up and right axis of a transform, so if all you wanna do is move the player in the direction it is facing towards, you could just take the transform.forward and multiply it by Input.GetAxis("Vertical")
. Hope it helps :D
https://docs.unity3d.com/ScriptReference/Rigidbody-rotation.html
Answer by Snowhouse · Apr 28, 2021 at 07:26 PM
this will turn your character cleanly, just set a float aimspeed to how fast you want to turn
if (Input.GetButton("Vertical")) { transform.Rotate(Vector3.up aimSpeed Input.GetAxis("Vertical") * Time.deltaTime); }
Your answer
Follow this Question
Related Questions
GameObject Rotation is always postive 0 Answers
How to keep a Gameobject in the same position after a transform.Rotate? 2 Answers
Rigidbody rotation vs Transform rotation 4 Answers
How to rotate child 3D object to match bend of mesh 1 Answer
Object not moving,Object not moving in any direction 1 Answer