|
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
(comments are locked)
|
|
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)" 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)
|

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?