x


how could you, while holding a button destroy then reinstantiate an object in original position after time?

ok basically im using the line renderer. im trying to move it(and activate it) when i press a button so that it looks like a shot(this works). What i am asking is what do u need to, while holding a button i want to remove it and make it spawn back to its original starting position, after a period of time so it mimics shooting bullet paths like a machinegun.

here is code:

public class TurretLight : MonoBehaviour {
public LineRenderer line;
public float bulletspeed = 5.0f;
public float lifetime = 1.0f;

// Use this for initialization
void Start () {

    line.renderer.enabled = false;

}

// Update is called once per frame
void Update () {

    if (Input.GetKey("f"))
    {
        line.renderer.enabled = true;

        if(line.renderer.enabled == false)
        {
        Instantiate(line, transform.position, transform.rotation);
        }

        transform.Translate(bulletspeed * Vector3.forward * Time.deltaTime);
        LineRenderer.Destroy(line, lifetime);

    }
    else if (Input.GetKeyUp("f"))
    {
        line.renderer.enabled = false;
    }

}

thanks please just reply if u no the code and how to do this ive tried everyting so far and would appreciate the help. sorry if a question like this has been answered already.

more ▼

asked Jan 19 '11 at 08:05 PM

Samir 1 gravatar image

Samir 1
147 15 15 26

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

First you should add these variables:

float time;
bool hasStarted = false;
bool hasEnded = false;
int timeDelay = 3; // Or however long you want your intervals

Then after that, deactivate the object and start the clock

void FixedUpdate() {
     if (!hasStarted && !hasEnded) {
          objectToMakeDisappear.gameObject.active = false;
          time = Time.time;
          hasStarted = true;
     }

     else if (hasStarted && Time.time >= time + timeDelay && !hasEnded) {
          objectToMakeDisappear.gameObject.active = true;
          hasEnded = true;
     }

     else if (hasEnded && Time.time > time + timeDelay) {
          hasStarted = false;
          hasEnded = false;
     }
}

This is untested and written in C#

more ▼

answered Jan 19 '11 at 11:36 PM

fireDude67 gravatar image

fireDude67
945 9 15 30

yeh ive figured it out i just made a new empty gameobject, and instantiated a prefab that has a lifetime so it will die, then i said if currenttime > timetospawn then instantiate new object :)

Jan 20 '11 at 08:01 PM Samir 1
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1672
x885
x184
x108
x7

asked: Jan 19 '11 at 08:05 PM

Seen: 714 times

Last Updated: Jan 19 '11 at 08:05 PM