- Home /
rotate parent to aim child
Hello all,
I have a child object that I want it's forward transform to point at another gameobject. I need to rotate it by rotating its parent object. How do I calculate what the rotation of the parent object should be to get the child looking in the right direction?
Thank you for your time.
edit: also, note that the child object in question will most likely not be located at 0,0,0 relative to the parent object. Not sure if that is relevant tbh.
You would need to rotate the parent object the same rotation as you would need to rotate the child.
Was that the issue or did you want to know how to find that rotation ?
Answer by siaran · May 27, 2014 at 05:16 PM
I found this solution (through another post on this forum that I cannot find anymore):
Quaternion relRot;
void Start(){
relRot = Quaternion.Inverse(transform.rotation) * child.transform.rotation;
}
void Update(){
Vector3 targetDirection = target.transform.position - child.transform.position;
Quaternion targetLook = Quaternion.LookRotation(targetDirection);
Quaternion childTargetRot = Quaternion.RotateTowards(child.transform.rotation, targetLook, rotateSpeed*Time.deltaTime);
Quaternion targetRot = Quaternion.Inverse(relRot) *childTargetRot;
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRot, rotateSpeed * Time.deltaTime);
}
which seems to do what I want. Quaternions aren't exactly my strong suit tho and calling Quaternion.RotateTowards twice seems a little weird. But this does what I want. Maybe I'll try your solution later but right now I am very happy to have it working and I don't want to touch it for a bit.
I've noticed that it gets a little jittery if the target gets too close, but otherwise it works rather well. Thanks!
Answer by Maerig · May 27, 2014 at 02:01 AM
You can compute this rotation by combining the parent look rotation and the child inverted local rotation :
public class CombinedRotations : MonoBehaviour {
public GameObject Parent, Child, Target;
private Quaternion m_parentToChild;
void Start() {
Vector3 angles = Child.transform.localEulerAngles;
m_parentToChild = Quaternion.Euler(-angles.x, -angles.y, -angles.z);
}
void Update() {
Quaternion lookRotation = Quaternion.LookRotation(Target.transform.position - Parent.transform.position);
Parent.transform.rotation = lookRotation * m_parentToChild;
}
}
Thanks! This worked nice in my case. Children in my Parents had very small or zero local position offset. Other variants didnt work perfectly because their calculations were based on positions and directions. This solution is much better, its simple and universal.
Answer by highpockets · Apr 02, 2019 at 09:35 AM
Ok, I found a pretty pure solution to this and the result works very well. I didn't want to have to add extra components (aim constraints or parent constraints) or extra game objects, etc.
My solution works as follows:
want child ("A") to aim at target (C") by rotating around parent ("B").
-since B is always going to be the same distance away from C, we can do some linear algebra and form a right angle triangle and use the distance from B to C as our hypotenuse and the point ("D") closest to B along the line that the forward vector of A produces will give us the point to form a 90 degree angle between D to B and the forward direction of A.
-Now the distance from B to C (hypotenuse) squared minus the distance from D to B (one side of the triangle) squared = the distance from D to the desired location of C relative to our setup ("E") squared. Once we have that we know that D + (C.transform.forward * distance to E) = E
-Now we take the cross product of D to E and D to C to get our xyz Quaternion rotation axis.
And we get our angle in radians as by getting the square root (dot product of D to E and D to E * dot product of D to C and D to C) + dot product of D to E and D to C. That is effectively our w rotation of quaternion.
-Now we build a new quaternion and normalize it new Quaternion(axis.x, axis.y, axis.z, angle).normalized and this is the reverse rotation of what is needed, thus we need to apply the inverse rotation to object B. B.transform.rotation = Quaternion.Inverse(ourNewQuaternion) * B.transform.rotation
That's it. Thought I would share in case someone else was looking to do the same.
Here is my code: (gun is my child, targetTrans is my target transform and spine is my parent)
Vector3 rightAnglePoint = Vector3.Project(spine.position - gun.position, gun.forward * 5); //Get point to create 90 degree angle for right angle
rightAnglePoint = gun.position + rightAnglePoint; //transform point to world space
float sideC = Vector3.Distance(spine.position, targetTrans.position); //Get hypotenuse
float sideA = Vector3.Distance(spine.position, rightAnglePoint); //Get sideA
float sideB = Mathf.Sqrt((sideC * sideC) - (sideA * sideA)); //Get sideB. (C squared - A squared = B squared)
Vector3 desiredRelTargetPos = rightAnglePoint + (gun.forward * sideB); //relative target point (if target were to rotate around spine to align with gun's forward direction)
Vector3 spineToTargetDir = targetTrans.position - spine.position; //spine to target position direction
Vector3 spineToRelTargetDir = desiredRelTargetPos - spine.position; //spine to desired target position relative to setup
Vector3 rotAxis = Vector3.Cross(spineToTargetDir, spineToRelTargetDir); //get rotation axis
float rotAngle = Mathf.Sqrt(Vector3.Dot(spineToTargetDir, spineToTargetDir) * Vector3.Dot(spineToRelTargetDir, spineToRelTargetDir)) + Vector3.Dot(spineToTargetDir, spineToRelTargetDir); //Get rotation angle
Quaternion inverseRot = new Quaternion(rotAxis.x, rotAxis.y, rotAxis.z, rotAngle).normalized; //Construct new Quaternion
spine.rotation = Quaternion.Inverse(inverseRot) * spine.rotation; //Apply rotation
Cheers
Wow man I don't know what this is doing but you've saved my a**. Thank you very much!
Answer by robochase · Jan 31, 2019 at 04:11 AM
Since this question was posed, Unity has introduced handful of classes that make this much easier. The class you're looking for is the ParentConstraint class.
In a nutshell, you want to put this component onto the parent, and add the child as a source (make sure to click "Activate" to seal the deal). This basically makes it so that the child is the new parent.
Hmm, are you suggesting to do this and then rotate around the real parent?? I think the question was to make the child point at a target but needing to rotate around the parent position to get there. If the child becomes the parent, this would be counter productive, or am I missing something? Sorry to dig up such an old post, but I want to do this, but can't find an elegant way to do so.
Your answer
Follow this Question
Related Questions
How to Rotate Relative To World 1 Answer
Add rotation from an angle 1 Answer
transform.rotation and localRotation automatically reset after rotating. 1 Answer
rotate object on transform.right 3 Answers
Flip over an object (smooth transition) 3 Answers