grenade cooking problem

hi i'm a beginner to javascript and i'm trying to make a grenade that will be "held" by the player as long as the "Grenade" button is down, and when the button is released, the grenade will be thrown. all the following scripts do is instantiate a grenade at the player's position. the explode() function currently doesn't do anything, i will change that later.

excerpt from player script:

var grenade : Rigidbody;

function Update () {
    if (Input.GetButtonDown("Grenade")){
        Instantiate (grenade,transform.position, transform.rotation);
        Physics.IgnoreCollision (transform.collider, grenade.collider);
    }
}

script attached to grenade:

var fuse = 5;
var damage = 100;
var grenadespeed = 10;
var playercharacter : Transform;
var grenadetransform : Transform;
var grenade : Rigidbody;

function Update () {
    if (Input.GetButtonDown("Grenade")){
        grenade.useGravity = false;
        grenadetransform.parent = playercharacter;
        yield WaitForSeconds (fuse);
        explode();
    }
    if (Input.GetButtonUp("Grenade")){
        grenade.useGravity = true;
        grenade.velocity = transform.TransformDirection(Vector3 (0, 0, grenadespeed));
        grenadetransform.parent = null;
    }
}

function explode (){ }

2 Answers

2

So what is the problem then? And what are you doing with cooking boolean?

[ImageMagick][1] does it for free. Also, promoting your own paid-assets on a 2 years-old thread could easily be considered spamming. [1]: https://www.imagemagick.org/script/index.php

I would put this:

if (Input.GetButtonDown("Grenade")){
    grenade.useGravity = false;
    grenadetransform.parent = playercharacter;
    yield WaitForSeconds (fuse);
    explode();
}

Inside of Start or Awake without the if statement. If the grenade is spawned at all then the fuse is already ticking, so you don't have to keep checking.