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?