- Home /
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!
a little trigonometry could help, some ATan of each object would give you their respective angle, then the difference between those angles is your desired value, not sure what ik is but at least rotating those objects can be solved this way
Answer by Danirey · Jan 10, 2014 at 05:10 PM
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! ;) ;) (and i'm the first)
Thanks man!
Answer by robertbu · Jan 10, 2014 at 05:09 PM
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;
Hi robertbu,
thanks for your answer. The spine isn't aligned with the parent gameobject, so if the character look at the target, the spine is almost perpendicular to that direction and the gun is child of the arm bone and is facing in other direction as well. So i think that the third option is more close to what i need. If i understand right, here we get a quaternion that goes from the actual gun direction, to the direction that will point the gun to the target, and then we add the rotation difference to the spine. So the spine will rotate until the gun face the target point in the world! Fantastic! I'll try as soon as i can and see what happens. Thanks a lot!
Ok! i tried and it rotates in the opposite direction.... It seems that this is not the answer after all! The theory in my head says that it should work, but it doesn't! $$anonymous$$aybe if i could get the angle between the objects in separated axis and then rotate each euler individually.... i don't know!
If i get the angle difference in the Y axis(vertical) between the weapon and the spine, and then rotate the spine these angle, i could test axis by axis it it works, but i cant get the data correctly. Vector3.Angle seems useless in this situation, and as i already said the $$anonymous$$ath is Black magic for me!
Please guys be patient with me! and try again ! ;)
I find the effort in answering rotation questions is about 80% figuring out how the geometry is setup and the desired result and on 20% co$$anonymous$$g up with a solution. I don't understand the geometry here, so I gave you some generic tools to approach the problem. Using euler angles is problematic. I don't understand why it would rotate in the 'opposite direction', but did you try reversing the parameters in the FromToRotation() call? Beyond that, I really need more information...drawing, screen shot, short video, simple project to play with...something to help me understand the relationship between the object and the problem you are trying to solve.
You're 100% right. So here we go!
The 3 images on the top are from left to right the forward of the character, the spine forward and the gun forward. the other two images are the result of point to the enemy's head. As you can see the spine is looking at the enemy, and the gun is ai$$anonymous$$g to the left. This difference is tha value i'm looking for. so i can compensate the rotation to the riaght and make the gun point to the enemy's head or whatever part of the body i choose!
The code is:
var q = Quaternion.FromToRotation(shotOrigin.forward, actualTarget - shotOrigin.position);
spine.rotation = q * spine.rotation;
shootOrigin is a transform and actualTarget is a Vector3. I've tried to modify the parameters but the results are always weird rotations.
This is the code i already have to make it appear that is ai$$anonymous$$g in the right direction, but at close range it looks at the moon!
var lookPos2 = actualTarget - shotOrigin.position;
var tempShotOriginRot = Quaternion.LookRotation(lookPos2);
tempShotOriginRot.eulerAngles+=Vector3(10,10,0);
spine.rotation.eulerAngles.x=tempShotOriginRot.eulerAngles.x;
Hope this makes more sense to may question!
And Thanks one more time! ;)
Helpful. I won't get a chance to study it until tonight.
Your answer
![](https://koobas.hobune.stream/wayback/20220613130023im_/https://answers.unity.com/themes/thub/images/avi.jpg)