I am trying to create code that makes a seperate game object (my gun) rotate around the player based on the angle that the player is looking at in 2D. I can not find any guides online so if I could get help Id appreciate it. My game is topdown.,I am trying to create code that makes a seperate game object (my gun) rotate around the player based on the angle that the player is looking at in 2D. I can not find any guides online so if I could get help Id appreciate it. My game is topdown

@MUG806

Hi, I was not asking that but I found the solution after a while. I was trying to keep my object at the right at a certain distance from my player.

This is fairly simple as long as you stop to understand.

The solution for this was to imagine the different positions around the player as a circle and putting that circle in a graph like this: image of the same problem in another website

You imagine a point as an x coordinate and a y coordinate.

There is a simple way to do this which is getting:

- Cosine of angle as x
- Sine of angle as y

However, that doesn’t account for the distance away from the player in which I want the weapon object to be in, so they need to be multiplied by the radius of the circle which is the distance away from the player

However to make this math work you need to make degrees into radians which can be done with this code here:

```
float angleInRadians = player.transform.eulerAngles.z * Mathf.Deg2Rad;
```

After doing that we do the before mentioned math and:

```
Vector3 coordinates = new Vector3(distanceFromPlayer * Mathf.Cos(angleInRadians), distanceFromPlayer * Mathf.Sin(angleInRadians), 0);
```

the “coordinates” variable is now a Vector3 (so a point in space with XYZ) which represents the X and Y values needed to make the object rotate around. Here is a graph I made which turns this code into math form and plugs it into a graph. graph. Feel free to play around with all the variables to see how they affect the graph. (a = angle in degrees, p = angle in radians, r = radius which is the distance from the player, x and y are the coordinates needed)

After the math part you want the weapon’s transform.position to equal your calculations plus the player’s transform.position so that it follows you around. This can be done with:

```
transform.position = player.transform.position + coordinates;
```

Although it goes around it doesn’t mantain the players orientation so the shots aren’t aimed. This is done with:

```
playerAngle = player.transform.eulerAngles;
transform.eulerAngles = playerAngle;
```

After everything your code should be looking similar to this:

```
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GunShareRotation : MonoBehaviour
{
public GameObject player;
Vector3 playerAngle;
public float distanceFromPlayer = 5f;
void Start()
{
}
// Update is called once per frame
void Update()
{
MouseOrientation();
SamePosition();
}
void MouseOrientation()
{
playerAngle = player.transform.eulerAngles;
transform.eulerAngles = playerAngle;
}
void SamePosition()
{
float angleInRadians = player.transform.eulerAngles.z * Mathf.Deg2Rad;
Vector3 coordinates = new Vector3(distanceFromPlayer * Mathf.Cos(angleInRadians), distanceFromPlayer * Mathf.Sin(angleInRadians), 0);
transform.position = player.transform.position + coordinates;
}
}
```

Good luck and I hope I have made this easier to understand or at least to copy paste!