- Home /
Animate a cube rotation with script only
Hello guys ! I have a little problem here. I'm making a game (working already) where my character is moving on a side of a cube and make the cube to rotate when he's reaching a face limit (exactly as in this game http://armorgames.com/play/13357). The thing is everything is working but I can't figure out how to make the rotation animations. I tried Lerp Slerp but it don't do anything except freezing for two seconds and then rotating as if I was doing transform.Rotate(0, 90, 0); A little help please ? =) Thanks in advance !
Answer by Phil74 · May 03, 2013 at 09:18 PM
Just to let people know how I managed to resolve my problem, I'm using the animation component but i'm creating the curves in a script while the program is running. Here is a sample :
public class CubeController : MonoBehaviour {
Quaternion resulting;
Quaternion beginning;
AnimationClip rotation;
AnimationCurve curve_x;
AnimationCurve curve_y;
AnimationCurve curve_z;
AnimationCurve curve_w;
GameObject cube;
int i;
void Start ()
{
cube = GameObject.Find("Map");
rotation = new AnimationClip();
}
void Animate(Quaternion beginning, Quaternion resulting)
{
rotation.ClearCurves();
curve_x = AnimationCurve.EaseInOut(0, beginning.x, 1, resulting.x);
curve_y = AnimationCurve.EaseInOut(0, beginning.y, 1, resulting.y);
curve_z = AnimationCurve.EaseInOut(0, beginning.z, 1, resulting.z);
curve_w = AnimationCurve.EaseInOut(0, beginning.w, 1, resulting.w);
rotation.SetCurve("", typeof(Transform), "localRotation.x", curve_x);
rotation.SetCurve("", typeof(Transform), "localRotation.y", curve_y);
rotation.SetCurve("", typeof(Transform), "localRotation.z", curve_z);
rotation.SetCurve("", typeof(Transform), "localRotation.w", curve_w);
cube.animation.AddClip(rotation, "rotate");
cube.animation.Play("rotate");
}
void left()
{
beginning = cube.transform.rotation;
cube.transform.Rotate(0, -90, 0, Space.World);
resulting = cube.transform.rotation;
cube.transform.rotation = beginning;
Animate(beginning, resulting);
}
}
Answer by Unitraxx · Apr 13, 2013 at 04:05 PM
The most primitive way is to put this in the Update() function :
transform.Rotate(0, rotateSpeed * Time.deltaTime, 0);
Of course you need to define rotateSpeed, try a public variable so you can play with it in the inspector.
(You should add some math though, to determine when a total of 90 degrees has been rotated.)
Sometimes the Rotate&keepTrack method can leave your count at 90, but the real rotation at almost 90. If it does, can key off of your own R float variable and set directly from that:
transform.rotation = Quaternion.Euler(0,R,0);
. But only works if you started facing dead ahead.
Answer by Phil74 · Apr 14, 2013 at 07:36 PM
Thanks for the answers but I can't put it in the Update function because i'm calling my rotations functions in OnTriggerExit where i detect if i'm exiting from left, right, down, or up. I tried also to put a thread.sleep in a loop from 0 to 90 where i'm doing 1 degrees rotations but it does a one time 90° on screen. I tried to force the update with camera.render but it doesn't do anything. This is driving me crazy !
Look at movement lerps (many, many are here.) They mostly involve setting a target (which you would do in Exit,) then having Update creep to the target. It seems odd, to have Update always spin you, but after it reaches the angle, the lerp won't do anything.
Your answer
![](https://koobas.hobune.stream/wayback/20220613100234im_/https://answers.unity.com/themes/thub/images/avi.jpg)