How can I rotate a Parent GameObject so a Child points towards the cursor?
The question says it all really, how can I rotate a parent GameObject so that a child of it points towards the cursor?
I know I can find the cursor's location with
public Camera theCamera; Vector3 mousePoint = theCamera.ScreenToWorldPoint(Input.mousePosition);
I assume we're in 2D here and the child has the same position as the parent.
What you can do is use Quaternion.LookRotation with Vector3.forward as first and from-parent-to-cursor-vector as second parameter. if set to the parent, it would look at the cursor. To to accommodate for the child, multiply the rotation (lhs) with the localrotation of the child (rhs) in that order lhs * rhs and assign the final rotation to the child.
Your answer
Follow this Question
Related Questions
Parenting an object causes it to change rotation 1 Answer
Problem with halt with rotating on y axis 1 Answer
How to make the rotation of a child object of object A match the rotation of object B ? 0 Answers
Broken rotation (c#) 0 Answers
Get LineRenderer positions or perform a transformation to an array of Vector3s 1 Answer