Hi!
I’m working on a TopDown-Shooter. Shooting works fine. Now I want to implement bullet shells, which pop out of the gun everytime the “Fire”-Button is pressed (it’s an Autorifle). I want the shells to fall on the ground, lay there for a certain time and disappear after a while.
This is my code so far:
public float fireRate;
private float nextFire;
private GameObject InstantiatedObj;
public GameObject shot;
public Transform shotSpawn;
public Transform MuzzleFlashPrefab;
public Transform shellEjectionPoint;
private GameObject InstantiatedShell;
void Update () {
string animString = animNumber.ToString ();
player = GameObject.FindGameObjectWithTag ("Player" + animString);
//shooting
if (Input.GetButton ("Fire_P" + animString) && Time.time > nextFire) {
nextFire = Time.time + fireRate;
InstantiatedObj = (GameObject)Instantiate (shot, shotSpawn.position, shotSpawn.rotation);
Destroy (InstantiatedObj, 3f);
//Muzzle Flash
Transform clone = Instantiate (MuzzleFlashPrefab, shotSpawn.position, shotSpawn.rotation) as Transform;
clone.parent = shotSpawn;
float position = Random.Range (0.0f, 0.0f);
float size = Random.Range (0.9f, 1.2f);
clone.localScale = new Vector3 (size, size, size);
clone.localPosition = new Vector3 (position, 0.4f, position);
Destroy (clone.gameObject, 0.1f);
//Shells
InstantiatedShell = (GameObject)Instantiate (shell, shellEjectionPoint.position, Quaternion.identity);
InstantiatedShell.GetComponent<Rigidbody2D> ().AddForce (-shellEjectionPoint.forward * Random.Range (180f, 220f));
Invoke ("laydown", 1f);
Destroy (InstantiatedShell, 3.0f);
}
}
“laydown” puts the Rigidbody2D to sleep:
void laydown(){
InstantiatedShell.GetComponent<Rigidbody2D> ().Sleep ();
}
What I got is this: Everytime the Fire button is pushed ONE shell flies out of the gun (in fact out of the screen…), every other shell after the first one is just created with no gravity and stays there for a certain time, before it’s destroyed. What I want is that every bullet flies out of the gun in an arc, stays there for a couple of seconds and disappears.
Could anyone be so gentle and help me please?
Thanks in advance!