- Home /
Top down radial shooting
I'm making a top-down game and I need to create different upgrades for the weapons, for example I need to shoot 3 (or more) bullets at a time from the player's position. How can I make the bullets radially go from player's position outwards? I saw this script that instantiates the bullets randomly over a circle, but I need them to be at precise positions on the circle and I also need to control the bullets "spread".
Answer by whydoidoit · Nov 14, 2012 at 10:47 AM
Yeah no problem. You want to set the rotation of the bullet to:
bullet.rotation = Quaternion.AngleAxis(-30, transform.up) * transform.rotation; //30 degrees one way,
Presuming that code is executing on the ship.
There's a tutorial on Quaternions and rotations on Unity Gems in the Getting Started section.
That's fine and it's working, but what if I want to make it more generic? For example, making it work with N bullets?
Well you might want to do something like this:
var startAngle = -$$anonymous$$athf.FloorToInt((n-1)/2)*30;
for(var i = 0; i < n; i++, startAngle += 30)
{
Instantiate(bulletPrefab, transform.position, Quaternion.AngleAxis(startAngle, transform.up) * tranform.rotation);
}
Almost there, but the bullets are slightly off, they should be aligned on the arc with the player: http://imageshack.us/f/23/schermata20121114alle11.png/