- Home /
Rotate an object (gun) around another (player) to always face the mouse position.
So I'm relavtivly new to Unity currently trying to code my first little game.
I have one object for the player and one empty object for the weapon which, as I press fire instantiates a projectile to fly forward. The weapon as a child of the player always starts +1 to the front of the player. What I want to do is rotate the weapon around the player to always face the mouse position. Note that the player itself does not rotate. I found the Transform.RotateAround method wich seems like a great starting point for this problem but I just can't figure out how to give it a smooth transition to the mouseposition just like this does:
Quaternion targetRotation = Quaternion.LookRotation(mousePosition - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, 10 * Time.deltaTime);
If I found a way to combine this code with the RotateAround method it would be perfect. I hope this little explanation of what I'm trying to do was clear and I hope somebody has a solution. Also if somebody wants to give me a better way of doing this feel free to tell me.