Rotating An Object On Its Axis Once

I can’t seem to find out how to do this just right. My basic idea is that there is a lever that, when the key “e” is pressed, will rotate to give it the effect that it is activated. This is a 2D game, so the lever is a plane. I have a custom pivot point at the middle-bottom of the plane.

How can I rotate the object JUST to the left one time when the “e” key is pressed? I have tried multiple different options, but they all cause the plane to go in strange directions. The only one that has worked so far is:

 function Update () {

      if (Input.GetKey ("e") {

           transform.Rotate (0, 45, 0 * Time.deltaTime, Space.World);



This did rotate the object gradually in the right direction as the “e” key was held, but this isn’t exactly the effect I’m going for. I want an automatic rotation, which will stop at a certain angle.

Any ideas? Help is greatly appreciated.



Well, I haven’t exactly solved my problem. So if anyone still wants to answer this question please do.
But I instead created an animation for the lever turning on and off, and tied them together and its actually functioning quite well.

float rotationleft = 360;
float rotationspeed = 10;

void Update()
    float rotation = rotationspeed * Time.deltaTime;

        rotation = rotationleft;
        rotationleft = 0;

Use Quaternion.Euler() to control rotation.

private var startRotY: float = 0;
private var endRotY: float = 0;
var currentRotY: float;

function Update ()

  if (Input.GetKey ("e") )
		currentRotY = 0;
		startRotY = 0;
		endRotY = 45;
		transform.rotation = Quaternion.Euler(0,0 ,0);
  currentRotY = Mathf.Clamp(currentRotY,startRotY,endRotY);
  transform.rotation = Quaternion.Euler(0,currentRotY ,0);


This should work correctly. It turns an object one time (and only one time) 90 degrees on the Z axis.

using UnityEngine;

public class RotateHalt : MonoBehaviour
    bool turn;
    if (Input.GetKeyDown(KeyCode.Q)) {
    			turn = true;
    			if(turn == true)
    			Vector3 rotation = new Vector3(0, 0, 90);
    			transform.Rotate (rotation);
    			turn = false;