# Firing bullets in all directions Unity C#

## Recommended Posts

Dear all, I'm makin a bullet shooter thing, at a certain position in Unity, it fires bullets 360 degrees around it.

private void SpawnProjectile(int _numberOfProjectiles)
{
float angleStep = 360f / _numberOfProjectiles;
float angle = 0f;

for (int i = 0; i <= _numberOfProjectiles - 1; i++)
{
// Direction calculations.
float projectileDirXPosition = startPoint.x + Mathf.Cos((angle * Mathf.PI) / 180) * radius;
float projectileDirYPosition = startPoint.y + Mathf.Sin((angle * Mathf.PI) / 180) * radius;

// Create vectors.
Vector2 projectileVector = new Vector2(projectileDirXPosition, projectileDirYPosition);
Vector2 projectileMoveDirection = (projectileVector - startPoint).normalized * projectileSpeed;

// Create game objects.
GameObject tmpObj = Instantiate(ProjectilePrefab, startPoint, Quaternion.identity);
tmpObj.GetComponent<Rigidbody2D>().velocity = new Vector2(projectileMoveDirection.x, projectileMoveDirection.y);

// Destory the gameobject after 10 seconds.
Destroy(tmpObj, 10F);

angle += angleStep;
}
}

Here's the code i gotten from a github page, I have trouble understanding some things. Regarding the area where Mathf.Cos and Mathf.Sin are, how are they used in calculating the bullet trajectory, from school I know sin x and cos x, which is opposite over hypotenuse and adjacent over hypotenuse, so how are they used in this case to calculate bullet trajectory? Oh and one more, can they be used interchangeably, Cos and Sin in the code, after all since they are evenly distributed it does not matter whether the x position or y position gets the sin or cos value?

##### Share on other sites

I think it's a little unfortunate that Sin and Cos are introduced in this way at school, because they're more fundamentally about the circle. Pop over to Wikipedia and watch this animation, which should clearly show you how to relate Sin and Cos to the circle. You can now see why to find the unit direction you need to send a projective off at angle theta is (cos(theta), sin(theta)). You can also see that there is a shift in their waves and it's this shift that means they cannot be used interchangeably.

Incidentally that code is needlessly inefficient in the way it calculates angleStep in degrees, and then converts it to radians each loop; it would be better to work in radians all the time.

##### Share on other sites

There's actually some redundant code here, but anyway, this code uses some trigonometry to solve it's problem by converting between two coordinate systems: the one we know and love, cartesian (x,y), and polar (r, angle), one that is more useful when you're working with circles (or ships apparently). These two coordinate systems are just used when you are trying to rotate in a circle on a 2d plane, when you need to get a position that you can use in world space (you can't use polar coords in unity world space!).

The formulas one uses to convert between these is the following:

To cartesian: x = r*cos(angle), y = r*sin(angle), if r is 1 you get a direction, which can still be useful; To polar: angle = atan2(y, x) (don't use atan, use atan2, this corrects the quadrants), and r is the distance from what you're interested in rotating around. All of this derives from ratios of a triangle, which you will want to master, and how it fits into a unit circle, which is also worthy of mastering.

What this code does is

1) (first two calculations) The programmer wants to get a vector that represents the trajectory of an object from the origin of the world to a point at the end of the direction point, where the direction point is connected to the start point (confusing, I know, but this is what he wanted to do). He does this by getting an angle, using anglestep, (remember: he wants to go in a CIRCLE) and converting it to cartesian coordinates. This gives him the "direction" of trajectory. He then adds it to the starting point, getting the projectileDirectionPosition components (this does not make up a direction vector, this is a world position; it adds the direction to the start point and makes some large vector that goes from the tail of starting point to the head/arrow of the direction vector)

2) (second two calculations) The programmer then turns the components into a vector object and subtracts each component by the startPoint (a + b - b, undoing what he did in the first two calculations), normalizing it (undoing the radius multiplication), then multiplying it by a speed (radius?!). He now has a direction vector, you can envision this as an arrow pointing out of the startPoint, and the longer it is, the more velocity it has. It can also be envisioned as a arrow sticking out of the origin; these point in the same direction -- they are just at arbitrary positions (a proper direction vector!).

3) (last of it) He then simply instantiates the object at the start point, and gives it a 0 rotation, and gives it the trajectory velocity he worked very tirelessly to calculate. Remember: imagine an arrow; doesn't matter where, but it shows the direction the object will go in when applied to velocity.

Edited by conditi0n

##### Share on other sites
11 minutes ago, conditi0n said:

What this code does is

Oh God, that's hilarious. I just sort of glanced over that part, but it's extraordinarily convoluted.

## Create an account

Register a new account

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634134
• Total Posts
3015751
×