x


How do you rotate a Bone towards a given target?

Given that I have a an imported Blender model, consisting of a cylinder Mesh, and two connected Bones, each half the Mesh length (and vertices weighted accordingly):

How do I rotate the end Bone towards a selected target's position, such that the associated Mesh points towards the target?

When I first created the model in Blender, they were all aligned on the Z-axis, with no rotation. When I saved them to a .fbx file, I specified no rotation on any axis. For whatever reason, the children GameObjects all have a different orientation than the top-level GO.

After importing, my Hierarchy looks like this:

oneTube (GameObject: standard rotation, x=0, y=0, z=0)
- - armature (GameObject: rotation of x=0, y=180, z=0)
- - - - oneBone (GameObject: rotation of x=90, y=90, z=0)
- - - - - - twoBone (GameObject: rotation of x=0, y=180, z=180)

Things I have tried, and couldn't get to work:

The LookAt() function:

twoBone.transform.LookAt(tmpTarget.transform.position);

The LookAt() function on the target in the Bone's Local Coordinate Space:

Vector3 invPos = twoBone.InverseTransformDirection(tmpTarget.transform.position);
twoBone.transform.LookAt(invPos);

Assuming that the Bone object was facing along its Forward vector, and rotating from there:

Vector3 invPos = twoBone.InverseTransformDirection(tmpTarget.transform.position);
Vector3 newDir = invPos - twoBone.position;
Quaternion newRot = Quaternion.FromToRotation(twoBone.forward, newDir);
twoBone.rotation = newRot;

I also tried all three axis vectors up/right/forward, none of which worked. And I tried using the Normal to all three axis vectors, again no joy.

Which brings up another question - what direction is the Bone considered to be facing? How can I determine that from its Transform?

more ▼

asked Feb 14 '10 at 01:18 AM

Cyclops gravatar image

Cyclops
7.1k 33 63 115

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

While not a direct answer to your question, I can recommend some resources that should help nevertheless:

What I'd try to do is use your model and try to make it work with the Head Look Controller. That way, you should be able to find out whether it's a problem with the setup of your model, or a problem with your scripting. And if it's a problem with your scripting, you can find out how it's done correctly in the source code of the Head Look Controller.

more ▼

answered Feb 14 '10 at 10:46 AM

jashan gravatar image

jashan
10.1k 25 40 116

(comments are locked)
10|3000 characters needed characters left

Ignore the bone rotation displayed in unity inspector. You need to change the bone rotation in blender to make sure it fits unity's coordinate system.

In blender, turn on "axes" in armature panel so you can see each bone's axis. In blender, z axis is "Forward", -x is "Up". Move around your bone root and tip to get the bone toward correct direction.

If for some reason you can't rotate bone freely (for example the bone is connecting a parent and a child), add a helper bone parenting the bone you want to control with minimal length, and make sure this helper bone's Forward and Up is in correct direction. Then you can control this helper bone via script in unity.

more ▼

answered Oct 11 '10 at 09:27 AM

nantas gravatar image

nantas
16 3

Your answer helped a lot. Z axis for bones in blender will point in the forward direction in Unity. Great for rotating a bone with Transform.LookAt()

Jan 19 at 04:29 PM Ronin6
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2166
x1278
x572
x143

asked: Feb 14 '10 at 01:18 AM

Seen: 5848 times

Last Updated: Jan 19 at 04:29 PM