- Home /
Rolling a cube
After creating a animation with unity with keyframes etc, is there anyway to write code to edit the precise coordinates of the keyframes so that the coordinates can be changed at runtime.
More precisely, i want to create a cube which rolls in different directions 1 cube distance at a time whenever the user presses up down left or right. If not by editing keyframes like i described, how should I go about it?
Animations would work best only if the cube is the same color on all sides, no texture that can be oriented visually.
Reopened. Yes, it's been asked and answered and a simple search on Archonn's part would have show it, but it was asked by others, and we have SOO many re-asked questions, to start trying to police them all seems daunting. I may be wrong, feel free to overrule.
Answer by Fattie · Sep 22, 2012 at 06:16 AM
FYI it's remarkably simple to "roll" a cube around in unity, physically. just ensure you are using absolutely realistic sizes, masses and physic-materials, for both the cube and the surface.
and then just apply forces to it, or perhaps torque, at the correct points.
the question is identical to the question "how to roll a 5cm cube around on my kitchen table?"
how would you do that? think about where you'd tap it with your finger, how hard, what sort of material you'd need to make it work best, and so on.
If you want to animate it, as DaveA. says just use RotateAround...
start at the center of the cube. go down one half cube width. now go in the direction you want to travel one half cube width. that's the point to use in RotateAround. make a vector that points to the left of the direction you want to go and that's the axis to use in RotateAround. (if you struggle with getting the "left" each time, just manually make a trivial array of four vectors being the relevant four "leftwards" hinge directions for the four directions you'll want to travel, then you can use that throughout.)
i suppose it's just
rotatearound(
center + desired x halflength + vector3.down x halflength,
Vector3.Cross(desired,Vector3.up),
90
)
Answer by DaveA · Sep 21, 2012 at 10:20 PM
As a coder I usually first think how I would do this in code. FWIW: Given the direction you want to move, find the edge on the ground that would be the pivot, and use RotateAround to do the movement.
Your answer
![](https://koobas.hobune.stream/wayback/20220613081323im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Mechanim Eliminates Gravity 0 Answers
Move cube forward 0 Answers
Can the animation editor create local rotational data? 3 Answers
Adding animation clips via script 2 Answers
Moving a Cube 1 Answer