- Home /
This question was
closed Aug 07, 2019 at 06:13 PM by
me-der for the following reason:
The question is answered, right answer was accepted
Question by
me-der · Aug 07, 2019 at 04:34 PM ·
2dparent-childlookparent and childlookatmouse
Parent and Child looking at mouse
I found this script to make object look at the mouse in the 2D game and it works fine:
var dir = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
Object.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
But when I made this:
var dir1 = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
var angle1 = Mathf.Atan2(dir1.y, dir1.x) * Mathf.Rad2Deg;
Parent.transform.rotation = Quaternion.AngleAxis(angle1, Vector3.forward);
var dir2 = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
var angle2 = Mathf.Atan2(dir2.y, dir2.x) * Mathf.Rad2Deg;
Child.transform.rotation = Quaternion.AngleAxis(angle2, Vector3.forward);
Only Parent was looking at the mouse.
When I turned off Parent's looking at the mouse child was looking at is as it should.
*Parent and child aren't in the same position
Comment
isn't it Camera.main.WorldToScreenPoint(Child.transform.position) ?
Follow this Question
Related Questions
[SOLVED]2D lookAt is off, any ideas? 1 Answer
Avoid scaling GameObject after parenting 2 Answers