I am trying to create some code that makes an attacking hitbox face the direction the player is walking. Currently getting the direction to face towards works fine, but actually changing the rotation of the object isn’t working. I would like the object to rotate instantly. This is the code I have so far:
void Update () {
verticalMovement = Input.GetAxisRaw ("Vertical");
horizontalMovement = Input.GetAxisRaw ("Horizontal");
if (verticalMovement == 0 && horizontalMovement == 0) {
// Debug.Log ("Idle");
zRotation = new Quaternion(0, 0, 180, 0);
transform.rotation = zRotation;
}
else if (verticalMovement == 1 && horizontalMovement == 0) {
// Debug.Log ("Up");
zRotation = new Quaternion(0, 0, 0, 0);
transform.rotation = zRotation;
}
else if (verticalMovement == 1 && horizontalMovement == -1) {
// Debug.Log ("UpLeft");
zRotation = new Quaternion(0, 0, 45, 0);
transform.rotation = zRotation;
}
else if (verticalMovement == 0 && horizontalMovement == -1) {
// Debug.Log ("Left");
zRotation = new Quaternion(0, 0, 90, 0);
transform.rotation = zRotation;
}
else if (verticalMovement == -1 && horizontalMovement == -1) {
// Debug.Log ("DownLeft");
zRotation = new Quaternion(0, 0, 135, 0);
transform.rotation = zRotation;
}
else if (verticalMovement == -1 && horizontalMovement == 0) {
// Debug.Log ("Down");
zRotation = new Quaternion(0, 0, 180, 0);
transform.rotation = zRotation;
}
else if (verticalMovement == -1 && horizontalMovement == 1) {
// Debug.Log ("DownRight");
zRotation = new Quaternion(0, 0, 225, 0);
transform.rotation = zRotation;
}
else if (verticalMovement == 0 && horizontalMovement == 1) {
// Debug.Log ("Right");
zRotation = new Quaternion(0, 0, 270, 0);
transform.rotation = zRotation;
}
else if (verticalMovement == 1 && horizontalMovement == 1) {
// Debug.Log ("UpRight");
zRotation = new Quaternion(0, 0, 315, 0);
transform.rotation = zRotation;
}
}
The rotation automatically jumps to -180 unless I am facing upwards. I am going to move it out of the update method soon and into its own method. Thanks for the help.