x


Shooting and changing bullet

Well, I'm trying to make 4 changeable bullets that will shoot properly forward in JS. I've tried everything, but It's still not working. Some tips what I should use? Bullets names: Fireball, Bubbleball, Snowball and a Waterball. Thanks

more ▼

asked Aug 11 '12 at 10:56 AM

Different gravatar image

Different
28 2 4 6

What have you tried? What isn't working?

Do you make a prefab for each bullet type and then instantiate them at the end of the gun with a velocity coming out of it?

Aug 11 '12 at 11:44 AM lozzaaa
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

What doesn't work as expected? The direction, the bullet selection? Anyway, I would add rigidibodies to the bullet prefabs, and define the prefabs in an array in the Inspector. To define the spawning position/rotation and direction, I would create an empty object "SpawnPoint", adjust its position and rotation and child it to the owner of the shooting script. Finally, would use a variable curBullet to show which bullet is currently selected:

var prefabs: GameObject[]; // assign the prefabs here in the Inspector
var curBullet: int = 0; // select the current bullet here

private var spawnPoint: Transform; // defines the position/direction
private var bulletSpeed: float = 20; // bullet speed

function Start(){
  // find the spawn point once at Start:
  spawnPoint = transform.Find("SpawnPoint");
}

// shoot the bullet selected by the indx parameter:

function Shoot(indx: int){
  var bullet: GameObject = Instantiate(prefabs[indx], spawnPoint.position, spawnPoint.rotation);
  bullet.rigidbody.velocity = spawnPoint.forward * bulletSpeed;
  Destroy(bullet, 5); // bullet self-destructs in 5 seconds
}

function Update(){
  if (Input.GetButtonDown("Fire1")){
    // shot the current bullet
    Shoot(curBullet);
  }
}

Change curBullet to select a different bullet.

more ▼

answered Aug 11 '12 at 11:43 AM

aldonaletto gravatar image

aldonaletto
41.5k 16 42 197

Looking good, but it's showing now "NullReferenceException UnityEngine.Transform.get_position () (at C:/BuildAgent/work/300357e52574df36/Runtime/ExportGenerated/Editor/UnityEngineTransform.cs:19) Shooting.Shoot (Int32 indx) (at Assets/Scripts/Shooting.js:15) Shooting.Update () (at Assets/Scripts/Shooting.js:23)" Maybe I'm putting this in the wrong place? Already tried on the player and the SpawnPoint. Every bullet has Rigidbody component.

Also, I wanted changing bullets directly in game with, for example, 1, 2 and 3 button. Thanks for helping anyway.

Newbie rage ;)

Aug 13 '12 at 06:21 AM Different
(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:

x3461
x1953
x275
x25

asked: Aug 11 '12 at 10:56 AM

Seen: 310 times

Last Updated: Aug 13 '12 at 06:21 AM