- Home /
Need help rotating the inside of a cube correctly (lerp gets me mid-way there)
Hi guys, i'm here asking for help, i am using some code to rotate a cube that the player sees from the inside, if the player presses CTRL and W,A,S,D he will rotate the cube in that direction, the rotation works but i had to Hard-Code the rotation variables in my script and that causes a strange rotation(sometimes the cube rotates to the side and then forward):
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TestRotation : MonoBehaviour
{
public Vector3 targetAngle;
public float speed;
private Vector3 currentAngle;
float time;
Vector3 direction;
public GameObject MainCamera;
bool coolDown;
string Tag;
public Image ctrl;
public void Start()
{
currentAngle = transform.eulerAngles;
direction = Vector3.zero;
coolDown = true;
}
public void Update()
{
ctrl.enabled = false;
if (Input.GetKey(KeyCode.LeftControl))
{
ctrl.enabled = true;
if (Input.GetKey(KeyCode.W) && coolDown)
{
direction = MainCamera.transform.forward;
}
else if (Input.GetKey(KeyCode.A) && coolDown)
{
direction = -MainCamera.transform.right;
}
else if (Input.GetKey(KeyCode.S) && coolDown)
{
direction = -MainCamera.transform.forward;
}
else if (Input.GetKey(KeyCode.D) && coolDown)
{
direction = MainCamera.transform.right;
}
RaycastHit hit;
Tag = "/";
if(Physics.Raycast(MainCamera.transform.position, direction, out hit) && direction != Vector3.zero && coolDown)
{
Tag = hit.transform.parent.parent.tag;
direction = Vector3.zero;
coolDown = false;
StartCoroutine(cooldown());
}
if(Tag != "/")
{
switch (Tag)
{
case "Bottom":
targetAngle = new Vector3(0, 0, 0);
time = 0;
break;
case "Forward":
targetAngle = new Vector3(90, 0, 0);
time = 0;
break;
case "Backward":
targetAngle = new Vector3(-90, 0, 0);
time = 0;
break;
case "Right":
targetAngle = new Vector3(0, 0, -90);
time = 0;
break;
case "Top":
targetAngle = new Vector3(180, 0, 0);
time = 0;
break;
case "Left":
targetAngle = new Vector3(0, 0, 90);
time = 0;
break;
}
}
Debug.Log(Tag);
Tag = "/";
}
if (time > 1)
{
currentAngle = targetAngle;
}
Rotate();
}
void Rotate()
{
time += Time.deltaTime;
transform.rotation = Quaternion.Euler(Vector3.Lerp(currentAngle, targetAngle, time));
}
IEnumerator cooldown()
{
yield return new WaitForSeconds(1f);
coolDown = true;
}
}
Note that i want the face that was hit by the raycast to be under my player(which is in the middle of the cube). The face that needs to be under the player is always correct, i just don't like the way the cube rotates and am wondering if there is something like a mathematical law to apply to get the right rotation without the need of hard-coding quaternions.
Answer by ryanmillerca · Nov 16, 2017 at 12:24 AM
The issue you're having is a result of the rotations and Lerp being calculated on a Vector instead of a Quaternion. You can calculate your bookmarked rotation angles as Vector3s and store them as Quaternions with Quaternion.Euler(), then use Quaternion.Lerp so that the rotations tween properly.
So, instead of
Vector3 targetAngle = new Vector3(0, 0, 90);
do
Quaternion targetAngle = Quaternion.Euler(0, 0, 90);
and instead of
transform.rotation = Quaternion.Euler(Vector3.Lerp(currentAngle, targetAngle, time));
do
transform.rotation = Quaternion.Lerp(currentAngle, targetAngle, time);
Rotations won't get messed up if they stay Quaternions!
Thank's most of the issues were fixed, by the way, is it strange that Lerp and Slerp produce the same exact result?
Answer by Legend_Bacon · Nov 16, 2017 at 11:11 AM
Hello there,
Ryan's answer is right, although in some cases I would recommend Quaternion.Slerp. If your rotations are far apart, Slerp usually looks better. For more information, click here.
Hope that helps!
~LegendBacon
Your answer
Follow this Question
Related Questions
Object detect my light RayCast and activating Lerp 0 Answers
How can I smooth the rotation using quaternion and raycast? 1 Answer
I want to lerp between different changing values 1 Answer
Raycast not detecting ANY HITS AT ALL when starting inside a collider. 0 Answers
Shifting a spawned bullet hole 0 Answers