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 ! :)
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);
}
}
Answer by Hellium · Dec 05, 2017 at 02:05 PM
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);
}
}
Thanks a lot for your help !
There was just a little issue with your script. Whenever the user presses a button twiche too fast, the rotation would get messed up. I fixed this by prohibiting input for 0.6 secounds after the function Get$$anonymous$$eyDown() was called.
Just in case anyone else ever needs that code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Camerascript : $$anonymous$$onoBehaviour
{
float speed = 20.0f;
private Transform cameraPivotPoint;
private Quaternion targetrotation;
private bool rotationisfinished = true;
private void Start()
{
cameraPivotPoint = GameObject.Find("CameraPivotPoint").transform;
}
private void Update()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A) && rotationisfinished)
{
targetrotation = Quaternion.Euler(0, +60, 0) * cameraPivotPoint.rotation;
rotationisfinished = false;
StartCoroutine(Timer());
}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.D) && rotationisfinished)
{
targetrotation = Quaternion.Euler(0, -60, 0) * cameraPivotPoint.rotation;
rotationisfinished = false;
StartCoroutine(Timer());
}
cameraPivotPoint.rotation = Quaternion.Lerp(cameraPivotPoint.rotation, targetrotation, Time.deltaTime * speed);
}
IEnumerator Timer()
{
yield return new WaitForSeconds(0.6f);
rotationisfinished = true;
}
}
Answer by InjustticeGaming · Dec 05, 2017 at 01:31 PM
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));
}
}
}