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.
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:
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.
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.
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.