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?
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.
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
center + desired x halflength + vector3.down x halflength,