Rotate an object with keyboard / on key press C# Unity

unity_rotate_josiahmunsey_coding

Place this script on a gameobject to rotate the object left and right when you hold down “Q” or “E.” Parent a camera to that object and now you can rotate your camera.


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Object_Rotate : MonoBehaviour
{ 

    public float speed = 10f;
    
    void Update()
    {

      if (Input.GetKey(KeyCode.Q))
        {
            transform.Rotate(Vector3.up, speed * Time.deltaTime);
        }

      if (Input.GetKey(KeyCode.E))
        {
            transform.Rotate(Vector3.up*-1, speed * Time.deltaTime);
        }


    }

}


// base code sourced from unity at https://unity3d.com/learn/tutorials/topics/scripting/spinning-cube 

Leave a comment