Answer the question
In order to leave comments, you need to log in
How to set a new rotation reference system for an object?
Good day. Game on unity, there is a cube that rotates when you press the arrows, the code was brazenly copied and pasted:
private bool isRotating = false;
IEnumerator Rotate( Vector3 axis, float angle, float duration = 0.3f)
{
Quaternion from = transform.rotation;
Quaternion to = transform.rotation;
to *= Quaternion.Euler( axis * angle );
float elapsed = 0.0f;
while( elapsed < duration )
{
transform.rotation = Quaternion.Slerp(from, to, elapsed / duration );
elapsed += Time.deltaTime;
yield return null;
}
transform.rotation = Quaternion.Euler(0,0,0);
isRotating = false;
}
void Update()
{
if(Input.GetKeyDown(KeyCode.UpArrow) && !isRotating)
{
isRotating = true;
StartCoroutine(Rotate(Vector3.right, 90));
}
if(Input.GetKeyDown(KeyCode.DownArrow) && !isRotating)
{
isRotating = true;
StartCoroutine(Rotate(Vector3.right, -90));
}
if(Input.GetKeyDown(KeyCode.LeftArrow) && !isRotating)
{
isRotating = true;
StartCoroutine(Rotate(Vector3.forward, 90));
}
if(Input.GetKeyDown(KeyCode.RightArrow) && !isRotating)
{
isRotating = true;
StartCoroutine(Rotate(Vector3.forward, -90));
}
}
transform.rotation = Quaternion.Euler(0,0,0);
Answer the question
In order to leave comments, you need to log in
you will have to deal with Quaternion, instead of copy-paste ((
https://docs.unity3d.com/ScriptReference/Quaternio...
then you will understand how to "add 90 degrees around the N axis to the current rotation"
the point is in these two lines
Quaternion to = transform .rotation;
to *= Quaternion.Euler( axis * angle );
I don’t remember how I did the same cube for myself)) try multiplication in a different order (it is important for quaternions what to multiply by))
to = Quaternion.Euler( axis * angle ) * to;
checked. you definitely need a global rotation vector, a different order of multiplication)) at school they lied that it does not affect))
plus at the end of the animation cycle, do
transform.rotation = to;
so that there are no angles of 88.9 and so on, otherwise the lerp does not twist every time a little bit and accumulates))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question