- Home /
Animating (simple rotation) armature by script
I tried to look around a bit, but I didn't find exactly what I was looking for... I'm new to Unity, so I'm basically just playing around with stuff, trying to understand how things work. I got a bit stuck on animating parts of a character by using the code.
1) how should I access various bones from the script? I attached my script to the player object, and I think the right hierarchy to access the neck transform should be Armature.upper_spine.neck.transform, by how my gameobject looks like, but it's not working at all. Instead it works [sort of] fine with GameObject.Find("neck") or by creating a script explicitly for the neck and attaching it to the "neck" object. Is there a more correct way to access various bones in the hierarchy?
2) in both the ways described above, I tried to reproduce what I already did with the whole mesh, that is rotating while a key is pressed (w\ the Q and E keys). If I do that directly on the gameObject (transform.Rotate), the whole character keeps rotating while the key is pressed, but on the neck it just rotates by the angle I specify as a parameter and stops there. This happens with both methods described in point 1) to access a bone (neck).
In addition to this, the neck seems to work only if I call the methods from LateUpdate, while the entire character works in the Update, too. (I know I should use coroutines, still working on that).
Why is this happening? Do objects and bones simply work differently? I found a good piece of code online that helped me a bit (saving the rotation in a variable, then looping the new rotation value in a coroutine), but I still don't understand the reason for this difference.
Thanks in advance!
Answer by softrare · Nov 09, 2011 at 07:51 AM
I don't know if that is what you are looking for but by animation by script I understand something like the following code. It is code I use for a demo for our asset store package of which there is a demo here. The script rotates the helix from a chopper.
public Transform topHelix; public float speed;
void FixedUpdate () {
float helixSpeed = 200f * speed;
topHelix.localEulerAngles= new Vector3(topHelix.localEulerAngles.x,topHelix.localEulerAngles.y,topHelix.localEulerAngles.z+helixSpeed);
}
Your answer
Follow this Question
Related Questions
Armature stuck on a single side after an animation 0 Answers
Problem with animation: they always start at the same rotation!! 1 Answer
Animate a cube rotation with script only 3 Answers
Rolling cubes one space and one rotation at a time, with a delay 2 Answers
Reseting armature's rotation in Unity 0 Answers