Hi , I trying to destroy an object after few seconds , when player throw a grenade , if the grenade is near or hit the enemy for example after explosion the enemy must be killed … ! here is my explosion and grenade code but I don’t know how can I write the code for destroy object !
my Enemy tag is “ROBOT”
var creationTime = Time.time;
var expplosionPrefab : Transform;
function Update ()
{
if(Time.time > (creationTime+3))
{
Destroy(gameObject);
Instantiate(expplosionPrefab , transform.position , Quaternion.identity);
}
}
thank you
I tried this but does not work fine
function robotDown ()
{
if (gameObject.tag == "ROBOT")
Destroy (gameObject, creationTime+3);
print("ROBOT IS DOWN");
}
I assume you must link your functions to begin with.
But to me it is a still bit unclear, what your problem is.
If you follow the TornadoTwins’ tutorials on the tube about the rocket turret game, you will get all the code you need to solve this…relatively little problem.
Why would you do a raycast instead of a sphereCast to find anything in range? Then you can get the distance using the Vector3.Distance() method and base your damage on that, for example. It will also be more performance friendly if you check against a layer instead of a tag. I think all the Physics cast (ray, sphere, etc.) take a layer mask anyway, so you can get it all at once. (?)
That’s why we contributors post here, to help novices (or learn something new of course) :o
I hope I’m not jumping-the-gun, but we are hoping to release a new for-sale package/tool which will handle this type of thing, among other stuff. With luck, I’ll be announcing it in a week or so. It will be reasonably priced for what it does, probably $20-$30 but we are still evaluating this, so don’t hold me to that!