x


Firing a curtain of bullets

I'm making a 2D shooter and wanted to know how to fire a curtain of bullets like in a bullet hell game from a single object. I'm trying to figure out how to alter a normal firing code to spread out in a fan shaped or random pattern if possible.

Edit: Code for firing //Fire rate of shots(Every # seconds, fire 1 shot) var fireRate: float =1.0; //Time to start shooting private var nextFire: float = 0.0;

function Awake()
{
    // set our starting x position
    originalX = transform.position.x;   
}
function Update(){
    //Move forward
    //transform.position.x += 1 * speed * Time.deltaTime;
    // if the user just pressed the "Shoot" button down (edit->project settings->input) & if the time since the game started is greater than the next fire time
    //As long as the time the game starts and the fire rate = nextFire and is less the Time.time, it'll fire.

    if(Input.GetButton("Shoot")&&Time.time > nextFire)
{
    audio.Play();
        // create a shot, to the right of us (so it doesn't clip into our collision), with 90 degrees rotation
    nextFire = Time.time + fireRate;
        //Instantiate(Shot, transform.position + Vector3.right*2, Quaternion.Euler(0, 0, 90));

    Fire();
    }

var BulletPrefab: GameObject;
var bulletSpeed = 100;

function Fire(){
  var rotation : Quaternion = Quaternion.identity;
  rotation.eulerAngles = Random.insideUnitSphere;
  var bullet : GameObject;
  bullet = Instantiate(BulletPrefab,transform.position,rotation);
  bullet.rigidbody.velocity = bullet.transform.forward*bulletSpeed;
}
more ▼

asked Aug 01 '10 at 02:17 AM

Persona gravatar image

Persona
246 74 85 96

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

If you are using particles for bullets you can use the random velocity setting.

If you are using rigid bodies for bullets you can use Random.insideUnitSphere to get a random direction for it to travel in.

e.g.

var BulletPrefab: GameObject;
var bulletSpeed = 100;

function Fire(){
  var rotation : Quaternion = Quaternion.identity;
  rotation.eulerAngles = Random.insideUnitSphere;
  var bullet : GameObject;
  bullet = Instantiate(BulletPrefab,transform.position,rotation);
  bullet.rigidbody.velocity = bullet.transform.forward*bulletSpeed;
}

If you wanted the bullets to move in a pattern then you can set the angle using some kind of math equation. Even very simple equations can result in interesting looking patterns. e.g. if you increment the y-rotation of the bullet fired by 10 degrees every frame they'll fan out like a spiral. If you then have a second set of bullets with their y-rotation incrementing in the opposite direction you'll get a double spiral that crosses over each other.

EDIT:

Changed rigid body to game object.

E.g. Maths:

yRotation : float = 0;

function IncrementAngle(){
   yRotation += 5;
   if(yRotation>359){
      yRotation = -359;
   }
}
more ▼

answered Aug 01 '10 at 10:06 AM

spinaljack gravatar image

spinaljack
9.1k 18 31 91

I'm getting an InvalidCastException: Cannot cast from source type to destination type. Can you give an example of the math formula as well? Math has never been my strong point.

Aug 01 '10 at 12:38 PM Persona

Try that now...

Aug 01 '10 at 03:14 PM spinaljack

this error came up: Actor::setLinearVelocity: Actor must be (non-kinematic) dynamic!

Also where do I put the math code? The update function moves it but can't have coroutines

Aug 02 '10 at 01:15 AM Persona

That error means your bullet doesn't have a non-kinematic rigid body. You can put the increment code where ever you want.

Aug 02 '10 at 01:26 AM spinaljack

Okay, I figured out that to make bigger changes in firing range I need to add to the random like this: rotation.eulerAngles = Random.insideUnitSphere * 25;

I mean how do I implement the math part, In the bullet script, in the player script?

Do I place Yrotation somewhere in the Instintate?

Aug 02 '10 at 02:31 AM Persona
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x572
x308
x152

asked: Aug 01 '10 at 02:17 AM

Seen: 2756 times

Last Updated: Aug 02 '10 at 01:05 AM