- Home /
Deleting Bullets after a few seconds
Hi
I'm making an FPS multiplayer game. I know that alot of objects will slow down the game, and since there will be alot of shooting, I thought that it would be good to delete the bullets after about four seconds. I didn't want to delete them after collision because I thought they might just go through the floor or other objects...
My question is how do I delete each cloned bullet four seconds after it was shot? Can you send me in a direction (a tutorial or article) or maybe post the rough out line of a script? Thanks for all the help!!!
Answer by Milad · Jun 23, 2011 at 03:08 PM
Just do it:
void Start()
{
Destroy(gameObject,DeathTime);
}
OR
void Start()
{
Invoke("Kill",DeathTime);
}
void Kill()
{
Destroy(gameObject);
}
It is easy!
And the best is that do not Instantiate the bullet with Mesh rendere On.make Mesh rendere Off to reduse the Draw Call and use a particle for bullet trace like Bootcamp.
Regards.
Answer by Jason B · Jun 23, 2011 at 02:44 PM
Something like this is very simple, so if you intend to make a full-blown multiplayer first-person shooter, you have some serious work ahead of you.
I only say this to help, because beginners often take on projects that are far beyond the scope of their abilities right out of the gate, then they feel hopeless and quit.
As to the question at hand, you want a variable to hold some sort of timer.
public float timer;
Then in your Update function, you'll want something to increment the timer.
timer += 1.0F * Time.deltaTime;
(1 gets added to timer per second)
Then you'll need a conditional check to destroy the bullet.
if (timer >= 4)
{
GameObject.Destroy(gameObject);
}
I do the opposite. I have a script called Perishable that I use for lots of things. I set lifeInSeconds to whatever and decrement it by Time.deltaTime. When there is no more life I Destroy.
$$anonymous$$ato $$anonymous$$ahto
Yep. Either way works. Ideally I would also have the time that it takes for something to be destroyed a public variable so that it can be changed inside the prefabs themselves, but this way may be simpler for someone learning the basics (not sure, just a guess).
Look, this is the simple and useful answer I was searching for.