Problem rotating and object with Quaternion.Lerp

Hey Guys

I have a problem trying to rotate an object around its y axis by pressing the A or D buttons. Whenever i try to rotate it ingame it starts to turn around randomly left and right on its y axis. What did i do wrong in this code ?

*edit: The Object needs to turn exactly 60 degrees on a button press and then stop.

Thanks in advance for your help ! :slight_smile:

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

public class Camerascript : MonoBehaviour
{
    Quaternion targetrotation;
    float speed = 1.0f;

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            targetrotation = GameObject.Find("CameraPivotPoint").transform.rotation;
            targetrotation.y -= 60f;
            Debug.Log("targetrotation " + targetrotation);
        }

        if (Input.GetKeyDown(KeyCode.D))
        {
            targetrotation = GameObject.Find("CameraPivotPoint").transform.rotation;
            targetrotation.y -= 60f;
            Debug.Log("targetrotation " + targetrotation);
        }
        GameObject.Find("CameraPivotPoint").transform.rotation = Quaternion.Lerp(GameObject.Find("CameraPivotPoint").transform.rotation, targetrotation, Time.deltaTime * speed);
    }
}

Unity uses Quaternions to handle rotation. You should not change the values of the x, y, z or w values directly if you don’t know the mathematics behind quaternions.

Here is how I would do it:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Camerascript : MonoBehaviour
 {
     float speed = 1.0f;
     private Transform cameraPivotPoint ;
     private Quaternion targetrotation;
 
     private void Start()
     {
         cameraPivotPoint = GameObject.Find("CameraPivotPoint").transform ;
     }
 
     private void Update()
     {
         if (Input.GetKeyDown(KeyCode.A))
         {
             targetrotation = Quaternion.Euler( 0, -90, 0 ) * cameraPivotPoint.rotation;
             Debug.Log("targetrotation " + targetrotation.eulerAngles );
         }
 
         if (Input.GetKeyDown(KeyCode.D))
         {
             targetrotation = Quaternion.Euler( 0, 60, 0 ) * cameraPivotPoint.rotation;
             Debug.Log("targetrotation " + targetrotation.eulerAngles);
         }
         cameraPivotPoint.rotation = Quaternion.Lerp(cameraPivotPoint.rotation, targetrotation, Time.deltaTime * speed);
     }
 }

They way I would do it is with a Rigidbody. Nice and easy to use.

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

public class Test : MonoBehaviour {
	float rotateSpeed = 50f;
	Rigidbody rb;

	void Start(){
		rb = GetComponent<Rigidbody> ();
		rb.constraints = RigidbodyConstraints.FreezeRotationZ | RigidbodyConstraints.FreezeRotationX;
		rb.angularDrag = 3f;
		rb.useGravity = false;
	}

	void FixedUpdate(){
		Turn ();
	}

	void Turn(){
		if (Input.GetKey (KeyCode.A)) {
			rb.AddTorque (new Vector3 (0, rotateSpeed, 0));
		}
		if (Input.GetKey (KeyCode.D)) {
			rb.AddTorque (new Vector3 (0, -rotateSpeed, 0));
		}
	}
}