- Home /
Pointing a child at an object using its parents rotation
Hi I have a system where an object's child has to face a target I require the entire object to be rotated so the child is facing the target but cant seam to work out the correct quaternion combination. I currently have this which seams to work fine except in some odd circumstances.
transform.rotation = Quaternion.RotateTowards(transform.rotation,
transform.rotation * Quaternion.FromToRotation(
child.transform.forward,
target.position-child.transform.position)
,temprotspeed * Time.deltaTime);
I know I basically need to apply a rotation that is zero when the child is facing the object but cant seam to work it out...and im sure its something rather easy...
Any help would be appreciated
Also just to note encase it wasn't obvious the child is facing a different direction to the parent. In this case I could just point the parents right vector towards the target, but I require a more general formula as not all the children will be pointing right...and most wont even be on a clean vector like right or up.
thought i had it figured out but this doesnt work although im sure it should
transform.rotation = Quaternion.RotateTowards(transform.rotation,Quaternion.FromToRotation(child.forward,transform.forward) Quaternion.LookRotation (transform.position-target.position,turnspeed Time.deltaTime);
$$anonymous$$y thoughts where to get the rotation difference between parent and child [so the child is effectively the forward vector] then rotate that to face the target....but doesn't seam to work either...
Ok Almost got this working
Vector3 childForward= target.transform.position - child.transform.position);
Vector3 childRight = Vector3.Cross (childForward, child.transform.up);
Vector3 childNewUp = Vector3.Cross (right, forward);
transform.rotation = Quaternion.RotateTowards (transform.rotation, Quaternion.LookRotation (childRight , childNewUp), rotationSpeed);
but this only works for one side as childRight and childNewUp always return the same no matter if childForward is to the right or the left...need some way to change the sign of one of the Cross's to relate to the facing of childForward or maybe change the maths somehow to incorporate the direction of childForward
I found this question and am abit manic about completing questions on unity reference so that they are useful for others one day... here is a solution to rotate a parent object, so that child transform upwards faces world vector3.up, although it could be any target vector:
cross = Vector3.Cross(Vector3.up, trunkispoiting);
angle = Vector3.Angle(Vector3.up, trunkispointing);
fractalTree.transform.RotateAround( Vector3.zero,cross,-angle);
Answer by ChiuanWei · Aug 08, 2011 at 03:20 PM
how about using this Function instead :
var relativePos = target.position - transform.position; var rotation = Quaternion.LookRotation(relativePos); transform.rotation = rotation;
unless I'm mistaken that would just point the parent at the object not the child?
var relativePos = target.position - child.transform.position; var rotation = Quaternion.LookRotation(relativePos); child.transform.rotation = rotation;
that will be child . have try?
Your answer
Follow this Question
Related Questions
How to fix Child Object Rotations.? 1 Answer
Child versus Parent rotations 3 Answers
force child rotation to zero? 2 Answers
Make a simple tree 1 Answer