Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Rolling cube while axis are changing

Discussion in 'Scripting' started by Vifi, Mar 30, 2018.

  1. Vifi

    Vifi

    Joined:
    Aug 5, 2013
    Posts:
    28
    Hi.
    I am wonder if anyone know a solution to my problem with changing axis while rolling cube.
    I am doing cube that moves in some direction (N,E,W,S) and rotating so it will stand on side that cube went.
    eg. if we have a cube originaly positioned on 0,0,0 when mowing forward 0,0,1 it should rotate 90 degrees in X axis (so it will stand on it's originaly front side).
    My problems is about axis. When i am doing first roll axis are changing because of rotation. After step forward axis Y is now directed to front (like axis z before), axis Z is directed to down. So then when i want to box go side (normaly on Z axis) i need to figure what axis is where and on what axis i should work so the rotation will be good.
    I tried to make two parents for every axis i need. So first parent rotate only in x axis, and his child rotate only on z axis and his child is box.
    xAxis
    --zAxis
    ------Box

    Then i rotate them (parents - x and z ), save their localrotation, reset their localrotation, combine them (x rotation form x parent, z rotation form z parent) and assign it to the box. So in my understanding it should leave box as it is ((i mean texture) because rotation is the same just on the other component (directly on box)) and it should also keep original axis there where i am using them to rotate (on parents x and z). Well, it doesn't work so obviously my understanding is wrong. Help me!
     
  2. Scabbage

    Scabbage

    Joined:
    Dec 11, 2014
    Posts:
    268
    I'll assume your box only moves in the X and Z plane, and will always be aligned to world space (is this assumption true?)

    The new rotation of your cube will simply be a Quaternion.Euler constructed from a +/-90 degrees on either the x or z axis. Since move positive X => rotate around Z axis, and vice versa, all you need to do is multiply the current target rotation by Quaternion.Euler(x, 0, z). Attach the following script to a cube in the editor and it will produce the rotation effect I think you're talking about when the WASD keys are pressed:

    Code (CSharp):
    1. using UnityEngine;
    2. public class BoxRotator : MonoBehaviour
    3. {
    4.     public float lerpSpeed = 10f;
    5.     float lerpTime = 1f;
    6.     Quaternion newRotation;
    7.     Quaternion oldRotation;
    8.     void Start ()
    9.     {
    10.         newRotation = oldRotation = transform.rotation;
    11.     }
    12.  
    13.     void Update ()
    14.     {
    15.         // get input from user and update state if need be
    16.         UpdateRotationState();
    17.         // if the lerp time is less than one, lerp to the new rotation
    18.         if(lerpTime < 1f)
    19.         {
    20.             lerpTime += Time.deltaTime * lerpSpeed;
    21.             transform.rotation = Quaternion.Lerp(oldRotation, newRotation, lerpTime);
    22.         }
    23.     }
    24.     void UpdateRotationState()
    25.     {
    26.         // Get the rotation, if any
    27.         float x = 0f, z = 0f;
    28.         if (Input.GetKeyDown(KeyCode.W))
    29.         {
    30.             print("Forward");
    31.             x = 90f;
    32.         }
    33.         else if (Input.GetKeyDown(KeyCode.S))
    34.         {
    35.             print("Back");
    36.             x = -90f;
    37.         }
    38.         if (Input.GetKeyDown(KeyCode.A))
    39.         {
    40.             print("Left");
    41.             z = 90f;
    42.         }
    43.         else if (Input.GetKeyDown(KeyCode.D))
    44.         {
    45.             print("Right");
    46.             z = -90f;
    47.         }
    48.         // if rotation is nonzero, apply it
    49.         if (x != 0f || z != 0f)
    50.         {
    51.             newRotation = Quaternion.Euler(x, 0f, z) * newRotation;
    52.             oldRotation = transform.rotation;
    53.             lerpTime = 0f;
    54.         }
    55.     }
    56. }