How to make a cube turn its faces while player controll it

Hi everybody,

Let me ask something, i want to make a cube that will turn its faces while player control it, for example.

If player press uparrow the cube must turn its face forward, left arrow turn its face left, right turn its face right. Does anybody have already played Edge, for iphone or ipod touch? I want to make something just like that.

Any ideias, i’m new in unity but i know the basics.

Thanks a lot.

Hello there!

What you need to do is set the rotation component in the object’s transform to a quaternion rotated in the correct direction, you can accomplish this the following way:

Vector3 direction = Vector3.zero;
dirextion.x  = (Input.GetKey(KeyCode.LeftArrow) ? -1 : 0);
dirextion.x += (Input.GetKey(KeyCode.RightArrow) ? 1 : 0);
direction.z  = (Input.GetKey(KeyCode.DownArrow) ? -1 : 0); 
direction.z += (Input.GetKey(KeyCode.UpArrow) ? 1 : 0);

if(direction.sqrMagnitude > 0)
    transform.rotation = Quaternion.LookRotation(direction);

In case you’re not familiar with the (bool ? trueValue : falseValue) method it works like this:
If the bool is true, the first value is used, if it’s false the second.
So in this case if you hold down both left and right arrow you’ll get a 0 as x.

thanks Myx i will try it!

Thanks a lot!

I’m giving this a go too. I can’t seem to get the above code to compile?

I’m given that it expects a semi-colon at the end of this line…

Vector3 direction = Vector3.zero;

Could you post the whole piece of code you’re using it in?

Myx,

It didnt work, actually it worked but not the way i wanted, this code is turning my cube forward, backyard, left and right. My need is spin my cube on x axis. For example when player press KeyUp, my cube must go forward moving on “vertical” axis with Input.GetAxis(“Vertical”) and turn one of its face forward, if player press LeftArrow my cube move on “Horizontal” axis and turn one of its face left, it just need to spin simulating an walk cycle, but spinning. This code you’ve posted is just turning my cube, not spinning it.

I’m stucked idon’t have any ideia how to do it.

Any ideias ?

Thanks again.

In other words, you want it to rotate gradually rather than all at once?

Yeahh! Thats it!

Alright, not such a difficult problem. Essentially what you might do is rotate a small amount each frame until you’ve achieved the right direction.

You can accomplish this “smooth” rotation using interpolation, which is supported fairly well within Unity. Here’s a bit of code from the documentation:

var from : Transform;
var to : Transform;
var speed = 0.1;
function Update () {
    transform.rotation =
      Quaternion.Lerp (from.rotation, to.rotation, Time.time * speed);
}

So if you set the “from” Transform to be equal to your original Transform at the time the player presses the button, and set your “to” transform to be equal to the transform that would result after the complete rotation (ie: the transform that Myx’s code produces), then the interpolation within Update will perform the gradual rotation for you.

Ahonek, thanks for your reply i’ll test and comment. Thanks again!