Snap the rotation of object

I am trying to snap the rotation of object to left and right, as i keep on clicking i dont know my there is some additional angle is being added everytime i click.

[36546-clicks+of+w.png|36546]

This is weird because after pressing W several time there is some additional angle is being added
104.83 is suppose to be 90, not 104.

what i am trying to do is to move object continuously in the right on x axis. and then with W, D, S keys i want them to rotate in there direction.( i am not using GetAxis because input.Getkey will be replaced by Touch after completion of game for mobile).

This is how i wont them to move

This is my script

using UnityEngine;
using System.Collections;

public class PlayerScript : MonoBehaviour {

	public float speed;
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		transform.Translate (Vector3.right * speed * Time.deltaTime);

		if (Input.GetKeyDown (KeyCode.W)) 
		{
			transform.Rotate (transform.rotation.x,transform.rotation.y,transform.rotation.z + 90f);
		}
	}
}

I am assuming the way you have your gameobject set up…

  • I notice you are rotating on the Z axis, I will do the same

  • 0 is up

  • 90 is right

  • 180 is back

  • 270 is left

    if (Input.GetKeyDown (KeyCode.W))
    {
    transform.localEulerAngles = new Vector3(0,0,0);
    }

    if (Input.GetKeyDown (KeyCode.A))
    {
    transform.localEulerAngles = new Vector3(0,0,270);
    }

    if (Input.GetKeyDown (KeyCode.S))
    {
    transform.localEulerAngles = new Vector3(0,0,180);
    }

    if (Input.GetKeyDown (KeyCode.D))
    {
    transform.localEulerAngles = new Vector3(0,0,90);
    }

By plugging in 0 for the other rotation values and not just adding 90 to our desired rotation, it will be impossible to get anything other than the desired values.

Since the rotation value is not being increasing/decreasing with increments and since the rotation value will not exceed 360, we can use eulerAngles instead of rotation. This allows us to plug in vector3 values(which I find easier to read/understand)

thank you @b1gry4n it worked like a charm !!