A
A
ANasty2019-08-20 17:38:38
C++ / C#
ANasty, 2019-08-20 17:38:38

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:

The code

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));
      
    }
    }


The problem was that with one rotation of the cube, the directions of the vectors also change, respectively, when you repeatedly click on one arrow, the cube constantly rotates in different directions. I didn’t think then and just after the turn I reset the rotation (I only needed animation) with a line. Now this is not suitable and you need to see the side on which the cube turned over. Question: So I turned the cube, for example by 90 degrees, how can I programmatically set that this position is now (0,0,0)? Or maybe there are some other solutions?
transform.rotation = Quaternion.Euler(0,0,0);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Gaydak, 2019-08-20
@PowerFull

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 question

Ask a Question

731 491 924 answers to any question