- Home /
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.
Answer by fireDude67 · Jan 19, 2011 at 11:36 PM
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#
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 :)
Your answer
Follow this Question
Related Questions
Checking the distance between an instantiated object and an existing object 1 Answer
Fire object towards mouse cursor 1 Answer
Setting the position of linerenderer 1 Answer
Instatiating a prefab in a random position 0 Answers
How can I get the x position for the left(and right) of the screen? 2 Answers