Mouse point code not working
The code below is roughly what is in my script. I recently started a new Udemy course. Unfortunately, it is quite old so recourses like Discord are quite slow. The project is a top-down shooter and this is the code to allow the gun to point towards the cursor. When in the game window the gun only moves when the character is moving, and at random angles. When I select a different window, like chrome, and have Unity open to the side, the code works perfectly. I am wondering if there is some sort of setting I have enabled, or disabled. Help would be much appreciated.
private void Update() { Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position; float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg; Quaternion rotation = Quaternion.AngleAxis(angle - 90, Vector3.forward); transform.rotation = rotation; }