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!

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:

using UnityEngine;
public class BoxRotator : MonoBehaviour
{
public float lerpSpeed = 10f;
float lerpTime = 1f;
Quaternion newRotation;
Quaternion oldRotation;
void Start ()
{
newRotation = oldRotation = transform.rotation;
}
void Update ()
{
// get input from user and update state if need be
UpdateRotationState();
// if the lerp time is less than one, lerp to the new rotation
if(lerpTime < 1f)
{
lerpTime += Time.deltaTime * lerpSpeed;
transform.rotation = Quaternion.Lerp(oldRotation, newRotation, lerpTime);
}
}
void UpdateRotationState()
{
// Get the rotation, if any
float x = 0f, z = 0f;
if (Input.GetKeyDown(KeyCode.W))
{
print("Forward");
x = 90f;
}
else if (Input.GetKeyDown(KeyCode.S))
{
print("Back");
x = -90f;
}
if (Input.GetKeyDown(KeyCode.A))
{
print("Left");
z = 90f;
}
else if (Input.GetKeyDown(KeyCode.D))
{
print("Right");
z = -90f;
}
// if rotation is nonzero, apply it
if (x != 0f || z != 0f)
{
newRotation = Quaternion.Euler(x, 0f, z) * newRotation;
oldRotation = transform.rotation;
lerpTime = 0f;
}
}
}