- Home /
The question is answered, right answer was accepted
Muzzle Flash
I have a texture of a muzzle flash and i'm going to apply it to an object, the thing i'm wondering is how do i make the object's rendering enable and disable rapidly while the mouse is held down.
Answer by robertbu · Dec 28, 2013 at 11:19 PM
Here is one solution. I imagine you want to start with the renderer for the flash graphic disabled:
#pragma strict
var speed = 20.0;
function Update() {
if (Input.GetMouseButton(0)) {
renderer.enabled = (Mathf.PingPong(Time.time * speed, 1.0) < 0.5);
}
if (Input.GetMouseButtonUp(0)) {
renderer.enabled = false;
}
}
ok so the way i asked for sucks, you gave me what i asked for and thanks, but i think i should make it so that ins$$anonymous$$d of turning it on and off it switches between 5 or 6 different texture every certain amount of time.
Here is a bit of code that uses a set of images. You need to initialize the array in the Inspector.
#pragma strict
var textures : Texture[];
var fps = 10.0;
function Update () {
if (Input.Get$$anonymous$$ouseButton(0)) {
renderer.enabled = true;
var i = (Time.time * fps) % textures.Length;
renderer.material.mainTexture = textures[i];
}
else {
renderer.enabled = false;
}
}
Here is a different script that displays a random texture rather than displays textures in order:
#pragma strict
var textures : Texture[];
function Start() {
InvokeRepeating("ShowTextures", 0.0, 0.1);
}
function ShowTextures() {
if (Input.Get$$anonymous$$ouseButton(0)) {
renderer.enabled = true;
renderer.material.mainTexture = textures[Random.Range(0, textures.Length)];
}
else {
renderer.enabled = false;
}
}
Follow this Question
Related Questions
Instantiating Muzzle Flash 1 Answer
How do i put lights with muzzleflash? 1 Answer
Muzzleflash Question 3 Answers
Why the particle system don't play? 2 Answers
How to make and use a muzzleflash 1 Answer