- Home /
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
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 co$$anonymous$$g out of it?
Answer by aldonaletto · Aug 11, 2012 at 11:43 AM
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.
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)" $$anonymous$$aybe 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 ;)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
rigidbody woe ? 1 Answer
Cube terrain with perlin noise 1 Answer
Click To Revert to Original Texture else Destroy script help 1 Answer
What is wrong with this script? 2 Answers