Help with weapon rotation top down 2D C#
I need help with my script. The purpose of the script is to rotate a gameobject that is a child of the player and have it follow the mouse. To do this, I use this script; void Update () { var mouse = Input.mousePosition; var screenPoint = Camera.main.WorldToScreenPoint(transform.localPosition); var offset = new Vector2(mouse.x - screenPoint.x, mouse.y - screenPoint.y); var angle = Mathf.Atan2(offset.y, offset.x) * Mathf.Rad2Deg; transform.rotation = Quaternion.Euler(0, 0, angle); }
The problem I have with this script is hard to explain, but essentially, the angle created is depending on where the mouse is in relation to (0,0), and not wherever the player is/where the camera is pointed. This means that if I mover right of the starting point, no matter where I click, it will fire right, because the mouse is to the right of (0,0,), even if it is on the left side of the screen. How do I get it so that it is centered on the player, not (0,0)? Thank you!
Your answer
Follow this Question
Related Questions
Rotate towards mouse pointer 1 Answer
2D Sprite issue when rotating towards mouse click location. 2 Answers
He wants to write a script on a page change gait from right to left or vice versa 0 Answers
Falling blocks skip animation while upside down 1 Answer
How to rotate Object with Mouse drag (specifications below) 2 Answers