I’m working on a script that will rotate an object 90 degrees. Here’s what I’ve got. I have been looking into it for a little while now and this is the most functional thing I created. I could use some help.
When I press Q or E the object just rotates in a jerking motion X amount of degrees depending on how high I put my turnSpeed. AND also the rotation only limited to +90 through -90 degrees. I want to be able to fluidly go in a full circle in 90 degree intervals. I was under the impression that Quaternion.Lerp would create a smooth rotation?
Could someone give me a hand?
P.S. I have tested it without the transform.rotation.y inside my Quaternion.Euler parameters and I get the same result.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewScript : MonoBehaviour
{
public float turnSpeed = 1f;
private Quaternion rotation = Quaternion.identity;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
float rotateDir = Input.GetAxisRaw("CameraRotate");
rotation = Quaternion.Euler(0, transform.rotation.y + (90 * rotateDir), 0);
if(rotateDir == 1)
{
Debug.Log("Turning right");
}
if(rotateDir == -1)
{
Debug.Log("Turning left");
}
if(Input.GetKeyDown(KeyCode.Q) || Input.GetKeyDown(KeyCode.E))
{
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, turnSpeed * Time.deltaTime);
}
}
}