# New Player Rotation

Hi all. I’ve been looking for answer on this one, but can’t see to find anything.

I’m trying to get the character to stay in the X angle rotation that it was moved to. I’ve figured that I will need to do an Update check on the players X rotation every frame and then once the player has stopped rotating, that will be it’s new rotation angle.

So at the moment, I have;

``````void Update()
{
IDLE_ANGLE = Quaternion.Euler(0, 0, 0);
}
``````

So the code in it’s entirety is;

``````private Rigidbody rb;

private Quaternion IDLE_ANGLE;

private float currentX = 0.0f;

public float speed;
public float rotate;
public float turn;

void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
IDLE_ANGLE = Quaternion.Euler(0, 0, 0);
}

void LateUpdate()
{
float moveAccelerate = Input.GetAxis("Vertical");
float moveTurn = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Jump");

Vector3 movement = new Vector3(moveTurn, moveVertical, moveAccelerate);

if (Input.GetKey("a"))
{
transform.Rotate(Vector3.up, -turn * Time.deltaTime);
transform.Rotate(Vector3.forward, rotate);
}

else if (Input.GetKey("d"))
{
transform.Rotate(Vector3.up, turn * Time.deltaTime);
transform.Rotate(-Vector3.forward, rotate);
}
else
{
transform.rotation = Quaternion.Slerp(transform.rotation, IDLE_ANGLE, rotate/5.0f);
}

}
``````

So, this makes the player return to it’s starting x rotation when either keys “a” or “d” are not pressed, but what’s the best why to make the player stay in the new x rotation?

Any help would be awesome!

What you have works. If you want it to stay in a range look into Mathf.Clamp(). Either way it looks like you do need a check (like an if statement) to make sure that idle is on and that the player shouldn’t rotate.

So I need the player to face the new direction rather than go back to the original. There must be some way I can make the X angle in the Quaternion Eular update to the new IDLE_ANGLE within this line of code.

``````void Update()
{
IDLE_ANGLE = Quaternion.Euler(0, 0, 0);
}
``````

I solved this by using the rigidbody.AddRelativeForce attribute. So now the code looks something like this.

``````public class PlayerController : MonoBehaviour {

private Rigidbody rb;

private Quaternion IDLE_ANGLE;
private float Accelerate;
private float MaxSpeed;
private float Speed;

public float rotate;
public float turn;

public Transform FrontWingL;

void Start()
{
rb = GetComponent<Rigidbody>();
IDLE_ANGLE = Quaternion.Euler(new Vector3(0, 0, 0));

Accelerate = 2f;
Speed = 1f;
MaxSpeed = 20f;

}

void FixedUpdate()
{

if (Input.GetKey("w"))
{
rb.AddRelativeForce(new Vector3(0.0f, 0.0f, Speed * Accelerate));
}

else if (Input.GetKey("s"))
{
rb.AddRelativeForce(new Vector3(0.0f, 0.0f, -Accelerate / 3));
}
else
{
rb.drag = 0.2f;
}

if (Input.GetKey("a"))
{
transform.Rotate(Vector3.up, -turn * Time.deltaTime);
FrontWingL.rotation = Quaternion.Euler(0.0f, 0.0f, Speed * Time.deltaTime);
}

else if (Input.GetKey("d"))
{
transform.Rotate(Vector3.up, turn * Time.deltaTime);
}

if (Input.GetKey(KeyCode.Space))
{
}

else if (Input.GetKey(KeyCode.LeftControl))
{