Make object rotate to certain direction

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.

Hi @BorisOZ !

The problem in the code is that it is treating Quaternions as if they were Euler Angles, but they are not the same thing. Every time you do:

zRotation = new Quaternion(0,0, someZValue, 0); 

You are not going to get the z rotation you expect because the values in a quaternion do not directly represent an euler rotation. Quaternions store rotation in a different way, hence the fourth value, they are a mathematical concept that uses complex numbers so they aren’t easy to understand at first sight (If I’m honest I don’t completely understand them either). If you want to read more about them you can do so here:

The solution to your problem is to replace the new Quaternion(0,0, someValue,0) part of the code with the static method Quaternion.Euler(0,0, someZValue) since that will take the euler angle values you provide and return a corresponding quaternion that the rotation property of your object can use. Like this:

zRotation =  Quaternion.Euler(0, 0, someZValue);

You can read about the Quaternion.Euler method here:

Hope this helps!