- Home /
Help: Making an object face a target
My character model's upper body should be turned towards a target. Problem is, the bone that controls the torso has a pivot as seen on picture 1.
TorsoBone.transform.rotation = Quaternion.LookRotation(Target.transform.position - TorsoBone.transform.position, Vector3.up)
Using Vector3.up as the "up vector" as specified on Quaternion.LookRotation produces the rotation as seen on picture two.
I have tried all possible Vectors, both World Vectors (Vector3s) and local ( TorsoBone.transform.*), it either didn't change anything or produced further weird rotations.
How can I make the bone with this pivot turn towards the target? :) (In all cases, the target is where the feet are pointing at) EDIT: In both pictures, the selected object is NOT the human model (which has a correct pivot), but the "torso bone" with it's faulty pivot (which can't be changed so it has to be done via script).
Answer by Cherno · Dec 08, 2013 at 11:58 PM
I finally got it working with an acceptable workaround:
I use a seperate empty gameobject as a torso controller but only temporarily insert it into the hierarchy as a parent to the actual torsoo bone, just AFTER making the model assume the firing stance/pose, and then after the shooting is done I parent the torsobone back to it's normal parent bone (pelvis in this case).
Answer by Tomer-Barkan · Dec 08, 2013 at 06:18 AM
It can be seen from your screenshot that the model you are using is facing upwards, and not forward (the blue arrow is forward, the green line is upwards).
This means that if you set your model to face a location, its left side (the blue line) will face that location.
I very strongly recommend that you fix the model so that it's facing forward (positive Z axis), because you will have a lot of issues in your game if you use it like this.
If that is not possible for some reason, I recommend making a parent empty game object to your player, and rotate the player manually so that it is facing the empty object's forward axis (positive Z again) - it should be 90 or 270 rotation on the X axis, according to the screenshots. Once you do that, apply all rotations and movements to the parent object. Make no changes whatsoever to the child transform. The parent object can be considered as your player from now on, since it is facing correctly.
Sorry I didn't state it, the selected object in both pictures is not the human model itself but the "torso bone". The human model has the pivot set up correctly, but the torso bone doesn't. It's not possible to change bone pivots in 3D Studio $$anonymous$$ax so this has to be somehow done by script :(
Then go for the parent object solution I described in my answer. Put your character inside a gameobject and rotate it so that it faces the parent's forward vector. Then make any transform changes (rotations and movements) on the parent object ins$$anonymous$$d.
The only other option would be to do all your calculations, so that ins$$anonymous$$d of facing the target, you will face 90 degrees to the right of the target. It is possible, but you can expect to have lots of issues during the work on this game.
$$anonymous$$aking a parent object for the whole character wouldn't do anything since the character model pivot is already correct. I already tried putting an empty gameobject into the bone hierarchy between the torso bone and it's parent, and it actually works, but then the animation doesn't work anymore because the hierarchy has changed (I presume).
I'm surprised there seems to be no simple solution to this since it has to be a common problem for everyone who uses bones/bipeds and wants to, for example, make the upper body face a target during shooting.
I'm not big on 3ds, but I'm surprised you're not able to change the orientation of the model so that it faces forward, and then re-import...
Ok, here's a solution to your specific problem, but I expect you to encounter other issues in the future using this model...:
Ins$$anonymous$$d of:
TorsoBone.transform.rotation = Quaternion.LookRotation(Target.transform.position - TorsoBone.transform.position, Vector3.up)
Use:
TorsoBone.transform.right = TorsoBone.transform.position - Target.transform.position;
I am indeed able to change the model's orientation (although I don't have to since it's already correct), but what I can't do is change the bone's rotation.
Anyway thanks for your help, for now I'll go with just rotating the whole model towards the target with is not as satisfying but has to suffice. $$anonymous$$aybe once I get a full license I can make it work via inverse kinematics.