Get angle difference between two objects to correct the position of a child object

Hi, guys!

I’m a little confused with Quaternions, eulers, angles etc… I’ve this problem: My character is facing forward. The animation rotates the spine a little to the right and the weapon is in the right hand and rotated to the left, so i want to rotate the spine until the forward of the gun points a certain point in the world. So i think i need to calculate the difference angle between the gun rotation and the target and apply it to the spine to rotate only the needed to make the gun point to the target. Of course when i make this, the animator component is disabled. I do that because i’ve unity free and can’t use ik. The game is turn based so i don’t need to play animation when aiming!

Thanks!

Thanks poncho!

IK is inverse Kinematics! For animations. With that i know that yo can still playing the animation but moving a hand to touch an object for example, but it is a unity pro feature.

I know that the trigonometry is the answer, but i’ve no idea of that. It would be nice that somebody could point me a little and then i’ll go alone from there. But i need to know what Mathf.Atan does and so on! I already take a look to the mathf functions relative to angles but i cant see more than that. Not everybody has been made to know about maths! :wink: :wink: (and i’m the first)

Thanks man!

I don’t fully understand your geometry, so I’ll give you some things to try. If your spine is created so that the ‘forward’ is in the direction of the object you want to aim at, you can do:

spine.transform.LookAt(aimPosition);

Typically you would only want the spine to rotate on one axis. If the spine is parallel to Vector3.up, then you can do:

var v = aimPosition - spine.transform.position;
v.y = 0.0;
spine.transform.rotation = Quaternion.LookRotation(v);

If you want a literal interpretation of what you are asking for would be this:

var q = Quaternion.FromToRotation(gun.transform.forward, target.transform.position - gun.transform.position);
spine.transform.rotation = q * spine.transform.rotation;