- Home /
Bulletshells not working as intended
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!
Answer by Dave-Carlile · Jul 01, 2015 at 01:23 PM
The problem is your InstantiatedShell
variable. It's global to the class, so every time you're instantiating a new shell, that variable references that latest one. So when your laydown
function is called 3 seconds later, its putting to sleep the last shell that was created.
You can fix the scoping problem by using a local variable instead of a class variable...
GameObject InstantiatedShell = (GameObject)Instantiate(shell, shellEjectionPoint.position, Quaternion.identity);
InstantiatedShell.GetComponent<Rigidbody2D> ().AddForce (-shellEjectionPoint.forward * Random.Range (180f, 220f));
This is declaring the InstantiatedShell
variable locally. The problem now becomes, how do you put that object to sleep after 1 second?
First, consider if you even need your laydown
function? Doesn't a physics object sleep on its own after awhile once it stops moving?
If you still do, your best bet is to probably add a script on the shell gameobject itself. Have it track the amount of time that has passed since it was created, and then go to sleep.
float timer;
void Update()
{
if (timer < 0) return; // nothing to do if we're sleeping
// otherwise track the time
timer += Time.deltaTime;
// go to sleep if we've reached our sleep threshold
if (timer >= 1.0f)
{
GetComponent<RigidBody2D>().Sleep();
timer = -1; // stop tracking time
}
}
Turn that into a full Monobehaviour and attach it to your bullet object. You can put the Destroy code in there as well and have the object destroy itself after 3 seconds.
Hi! Thanks for the reply! I couldn't be on my computer the past few days. Arrived today from a trip, saw your suggestion, implemented it and it works like a charm! Thanks a lot!
Answer by LaneFox · Jul 01, 2015 at 01:29 PM
I suggest using a Particle System.
Make it emit a Mesh with the material you want on the shell, configure the physics the way you want, set it up on Burst style, point it in the direction you want, keep a reference to it and every time you fire the weapon do shellSystem.Emit(1)
(docs).
Works great =)
Thanks for your reply! I'm new to Unity and haven't been brave enough to work with particles. But thanks for the hint!
Your answer
![](https://koobas.hobune.stream/wayback/20220613201611im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
InvokeRepeating affecting parts of script 1 Answer
Draw a line and instantiate it as a game object 0 Answers
Checking if object intersects? 1 Answer
Variable isn't assigned, even though I have assigned it in the Start() method 2 Answers
When flipping player, instantiated objects spawn on different spot. 2D shooting 0 Answers