- Home /
Unity3D C# enable renderer for a few seconds
Hello, I've been trying to create my muzzle flash, I downloaded gimp, followed a tutorial on how to create the muzzle flash, and now I got it, and I tried to write a script and I "kinda" managed to do so but I got a problem with my script, the renderer is shown all the time when Fire1 is held down and I simple want it to appear for something like 0.1/0.2 seconds and then disappear, I tried checking out the unity script reference for WaitForSeconds but I saw someone say it was very expensive (whatever that means) for the game. I would be greatfull if anyone could help me, Im posting the script below
if(Input.GetButtonDown("Fire1") && Time.time >= timestamp)
{
if(ammoPerClip > 0)
{
Rigidbody bulletInstance;
bulletInstance = Instantiate(Bulletprefab, barrelEnd.position, barrelEnd.rotation) as Rigidbody;
bulletInstance.AddForce(barrelEnd.forward * 6000);
timestamp = Time.time + timeBetweenShots;
audio.Play ();
ammoPerClip--;
flash.renderer.enabled = !flash.renderer.enabled;
skottljus.light.enabled = !skottljus.light.enabled;
}
}
if (ammoPerClip == 0 && Input.GetButtonDown ("Fire1"))
{
audio.PlayOneShot(click);
}
if (Input.GetButtonUp ("Fire1"))
{
flash.renderer.enabled = false;
skottljus.light.enabled = false;
}
}
How would I go about to do this?
Answer by Freaking-Pingo · Apr 14, 2014 at 10:26 AM
You can look into Unity's "Time" class. There are variables such as Time.time which gives you the time since the execution of the game, and there is also Time.deltaTime which gives you the time since the last update was executed. Try look into these variables as I am sure this will help you.
I tried putting yield WaitForSeconds(0.05) (I saw this in a java tutorial) and I thought like, Java is almost the same as C# right? so I tried, didnt work... <.<
Is it possible to check if getbuttondown is held down for a amount of seconds, if it is, then turn off renderer?
Your answer
Follow this Question
Related Questions
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
how to assign an array of renderer to array of gameObjects 0 Answers
Changing material of objects by the tag in different scenes 0 Answers
Material class use general question 1 Answer