- Home /
Rotating a bone via scripts
Goal: To be able to rotate bone in script. shoulder is the bone I'm trying to rotate.
Problem: No errors, but bone either
1.) doesn't rotate in game
or 2.) mesh doesn't move with bone
Here's my script:
shoulder is a Transform var
function PointToMouse(){
if(shoulder){
print("shoulder");
}
var playerPlane = new Plane(Vector3.forward,transform.position);
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hitdist = 0.0;
if (playerPlane.Raycast (ray, hitdist)) {
var targetPoint = ray.GetPoint(hitdist);}
mouseVec = Vector3(targetPoint.x - transform.position.x,targetPoint.y - transform.position.y,0);
zAngle = Vector3.Angle(mouseVec,Vector3(0,-1,0));
if(zAngle>minZ && zAngle<maxZ){
print(zAngle);
shoulder.transform.localEulerAngles = Vector3(0,0,zAngle+270);
}
}
It prints shoulder and prints zAngle and zAngle is correct. Also, when shoulder is an Empty GameObject with a mesh as a child, the mesh will rotate. So I know the logic is right...
Any help?
Aha! If I don't play the animations, the bone will rotate. How can I override the animation??
Answer by nixtwiz · Oct 05, 2012 at 12:37 AM
Put the rotation code in LateUpdate. LateUpdate is called after the animation is applied, so you can override the animation movement. HOWEVER, if you do this the rotation cannot rely on the previous rotation of the bone, so no lerps or anything of that sort. You'll get a jittering effect if you try to use a lerp with the previous rotation of the bone since the animation is still being applied before the next time the lerp is called.
Thank you for this good answer. Never knew LateUpdate existed.
sweet Geezus. Thanks nixtwiz, you are like mother tereza to me right now. really helped me. I was having the same problem, although translation was actually possible while the mesh had animation on. weird?
could you please advise me how to save the rotation for the follwing frames when I apply the rotation? as it keeps jettering and flickering
O$$anonymous$$G i spent 12 hours looking for the solution and you answered this in 2012 :)
Thanks a ton man :)
Your answer
Follow this Question
Related Questions
Added rotation AFTER the rotation from animation clip ( rewarding 400 points to whoever solves this) 1 Answer
How to make a bone rotate facing towards a given Transform? 0 Answers
How to prevent Z axis rotation ? 1 Answer
How can I rotate a bone from script? Applications: Move crane arm, point gun... 2 Answers
Updating rotation of client not working - strange error 0 Answers