Hi there.
I’m hoping someone can help.
I’m rotating a cube (C script), and am using the arrowkeys for this.
However, after a few random key presses, up might not rotate the cube up anymore. Left could rotate the cube in any direction etc.
What I’m after is for ‘up’ to always be an upward rotation (and likewise all other keys), no matter how the cube is currently rotated.
Here’s my code so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class rotate_c : MonoBehaviour {
public float smooth = 1f;
public float howmuch = 90f;
private Quaternion targetRotation;
void Start(){
targetRotation = transform.rotation;
}
void Update () {
if (Input.GetKeyDown(KeyCode.LeftArrow)){
targetRotation *= Quaternion.AngleAxis(howmuch, Vector3.up);
}
if (Input.GetKeyDown(KeyCode.RightArrow)){
targetRotation *= Quaternion.AngleAxis(howmuch, -Vector3.up);
}
if (Input.GetKeyDown(KeyCode.UpArrow)){
targetRotation *= Quaternion.AngleAxis(howmuch, Vector3.right);
}
if (Input.GetKeyDown(KeyCode.DownArrow)){
targetRotation *= Quaternion.AngleAxis(howmuch, -Vector3.right);
}
transform.rotation= Quaternion.Lerp (transform.rotation, targetRotation , 5 * smooth * Time.deltaTime);
}
}
Oh, and here’s a zip file of my project.
https://www.tsites.co.uk/downloads/quarternion_rotation.zip
A huge thanks if anyone can help. Until then, stay safe & wishing you all well.