- Home /
PlayerController rotation axis
Hey guys, so I'm programming a PlayerController for a game and I want to let the player rotate in the direction that he is looking in (it's third person but with a fixed camera angle). So when for example Input.GetAxisRaw("Vertical") == 1 && Input.GetAxisRaw("Horizontal") == 0 the player should turn into the forward direction. But I want him to turn like 45 degrees aswell when Input.GetAxisRaw("Vertical") == 1 && Input.GetAxisRaw("Horizontal") == 1. So he goes diagonal and I want to rotate him towards it. I could of course use a bunch of if-statements to detect every possibilty. But is there some way I could do this in one line of code by putting both GetAxisRaw into transform.Rotate(0f, someCode, 0f) ?
Looking forward to your solutions
This is my current solution:
void Rotate ()
{
if (Input.GetAxisRaw ("Horizontal") > 0) {
if (Input.GetAxisRaw ("Vertical") > 0) {
float angle = $$anonymous$$athf.LerpAngle (transform.eulerAngles.y, 45f, rotationSmooth);
transform.eulerAngles = new Vector3 (0f, angle, 0f);
} else if (Input.GetAxisRaw ("Vertical") == 0) {
float angle = $$anonymous$$athf.LerpAngle (transform.eulerAngles.y, 90f, rotationSmooth);
transform.eulerAngles = new Vector3 (0f, angle, 0f);
} else if (Input.GetAxisRaw ("Vertical") < 0) {
float angle = $$anonymous$$athf.LerpAngle (transform.eulerAngles.y, 135f, rotationSmooth);
transform.eulerAngles = new Vector3 (0f, angle, 0f);
}
} else if (Input.GetAxisRaw ("Horizontal") == 0) {
if (Input.GetAxisRaw ("Vertical") > 0) {
transform.rotation = Quaternion.AngleAxis (0f, Vector3.up);
} else if (Input.GetAxisRaw ("Vertical") < 0) {
transform.rotation = Quaternion.AngleAxis (180f, Vector3.up);
}
} else if (Input.GetAxisRaw ("Horizontal") < 0) {
if (Input.GetAxisRaw ("Vertical") > 0) {
transform.rotation = Quaternion.AngleAxis (315f, Vector3.up);
} else if (Input.GetAxisRaw ("Vertical") == 0) {
transform.rotation = Quaternion.AngleAxis (270f, Vector3.up);
} else if (Input.GetAxisRaw ("Vertical") < 0) {
transform.rotation = Quaternion.AngleAxis (225f, Vector3.up);
}
}
}