- Home /
How to make gun rotate around player based on mouse position
i want the gun to rotate around the player based on the mouse's position relative to the player like this:
this is my setup for the rotation part:
the gun is offset from the pivot point, which is locked on the player
how can i find the position of the mouse relative to the player and then rotate the gun to point that way?
Answer by Barretts · Jun 29, 2021 at 06:13 AM
Vector3 difference = Camera. main. ScreenToWorldPoint(Input. mousePosition) - transform. position; float rotation_z = Mathf. Atan2(difference. y, difference. x) * Mathf. Rad2Deg; transform. rotation = Quaternion. Euler(0f, 0f, rotation_z + offset);
Answer by ravenclarico · Jun 29, 2021 at 03:16 AM
quaternion.lookrotation
sorry i should have been more specific. i need a way to find the position of the mouse in relation to the player too. thank you for this though
Your answer
![](https://koobas.hobune.stream/wayback/20220613035615im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
2d character, how to limit rotation to z, with character looking left? 0 Answers
Point an arrow at the mouse position, 2.5D 1 Answer
(solved) 2d sprite rotation towards mouse 1 Answer
How to control a 2d character in an top-down android game with the standard joystick? 1 Answer
How to smoothly rotate object in direction of analog joystick? 1 Answer