(Hopefully) simple solution to this I'm sure! Locking "Gunpoint" to player

Hi guys,

Nice and simple one here that’s totally got me stumped. All I want to do is rotate my player’s gun location relative to the player’s orientation. Here’s a video of the current state along with the code used at this point.

The large, outer square in the video is the player’s mouse cursor, which they rotate and look towards, the smaller inner square is the gun location - where bullets spawn from.

transform.position.x = player.transform.position.x + Mathf.Sin(player.transform.rotation.y);
transform.position.z = player.transform.position.z + Mathf.Cos(player.transform.rotation.y);

Any help is appreciated!!

If you want the gun to point at the cursor, you can simply use Transform.LookAt().

gun.transform.LookAt(cursor.transform);

Thanks but not quite. The bullets shoot towards the mouse that’s no problem. However the spawn point of the bullets should move around the player to prevent them shooting from behind and through them for example.

EDIT: Right… feeling a little silly now! Of course, there’s a simple solution that requires no maths (trust me to over-complicate things) I now just use player.transform.position + transform.forward;

Thanks anyway!