- Home /
spawn muzzle flash with random rotation?
Hi. I have this code here GameObject flash = Instantiate(muzzleFlash, position, rotation) as GameObject;
and I'm trying to make it spawn the flash(MuzzleFlash) at the position and rotation of the bulletSpawn gameObject I have at the end of the barrel on my weapon. but unfortunately I can't figure out how to change the rotation to something random. Meaning that it rotates the X to something random.
I hope that made sense...
Just look up "unity random rotation y" (people seem to ask about Y more.) If you can't change the Y to an X (or you thought you wanted X, but it isn't doing what you thought,) ask something with those details.
You could use Quaternion.Euler(random, orig_y, orig_z)
and pass it as the rotation to the instantiate:
GameObject flash = Instantiate(muzzleFlash, position, Quaternion.Euler(Random.Range(0f, 360f), bulletSpawn.transform.eulerAngles.y, bulletSpawn.transform.eulerAgnels.z) as GameObject;
Answer by aldonaletto · Oct 27, 2013 at 02:21 PM
The old-and-good-but-no-more-available FPS Tutorial used an interesting approach: instead of instantiating the muzzle flash every shot (what allocates memory), the muzzle flash was an object childed to the gun and with its renderer disabled. When shooting, the mesh was rotated randomly about its local Z axis and the renderer was enabled for just one frame. You could use the same trick with this code, which should be part of the weapon script:
public var muzzleFlash: Transform; // drag the muzzle flash object here
private var muzzle = false; // muzzle flash enabled last frame?
function Start(){
// make sure the muzzle flash is initially disabled:
muzzleFlash.renderer.enabled = false;
}
function TurnOnMuzzle(){
// rotate the muzzle flash randomly about Z:
muzzleFlash.Rotate(0, 0, Random.Range(0, 90));
muzzleFlash.renderer.enabled = true; // turn on muzzle flash
muzzle = true; // tell the code to turn it off next frame
}
function TurnOffMuzzle(){
if (muzzle){ // if muzzle flash enabled last frame...
muzzleFlash.renderer.enabled = false; // turn it off
muzzle = false;
}
}
TurnOnMuzzle must be called when you shoot - after checking ammo, shot interval etc. (flashing when you have no ammo is embarrasing...). Shooting or not, you must call TurnOffMuzzle every Update before doing anything in this script: this ensures that the muzzle flash lasts for just one frame:
function Update(){
TurnOffMuzzle(); // call this before anything in Update!
// rest of Update
}
Thanks for posting this. I couldn't find the old-and-good-but-no-more-available FPS Tutorial unfortunately. but knew that it had a good way to make the muzzle flash work. I think I'll probably change my code to enable/disable the renderer on the muzzle flash ins$$anonymous$$d of instantiating it as you said, it would take a lot of memory. I changed the values of Random.Range(0, 90) to (-30, 30) for some reason the muzzle flash would rotate to a 90 degree angle sideways.
I just copied this line: muzzleFlash.Rotate(0, 0, Random.Range(0, 90));
Into the script I have attached to the muzzle flash so that when it created is starts with a random rotation.
Thanks for the help ;)
Just a small tip to make it look nice without instantly disappearing, you can do something like this to fade the materials opacity to 0 and then turn off the renderer, and call a function to reset the opacity whenever the gun fires.
function UpdateColor() {
var renderers = transform.GetComponentsInChildren($$anonymous$$eshRenderer);
var color = Vector4(opac,opac,opac,opac);
for(var renderer : $$anonymous$$eshRenderer in renderers){
renderer.material.SetColor("_TintColor", color);
}
}
function Update() {
if(firing == true){
UpdateColor();
opac -= Time.deltaTime * 50.0;
if(opac <= 0.0){
opac = 0.0;
UpdateColor();
firing = false;
}
}
}
Then just have a function reset opac to 1.0 and firing to true.
Answer by thenachotech1113 · Oct 27, 2013 at 04:02 AM
try something like this:
GameObject flash = Instantiate(muzzleFlash, position, rotation.x, Random.Range(1, 360), rotation.z) as GameObject;
i have not tried this code because i do not have unity installed in this pc, though im pretty sure something like that should do the trick. hope it helps
Sorry, there was an error: error CS1501: No overload for method Instantiate' takes
5' arguments I tried adding brackets "(")" around the rotations but that didn't work either. :/
Just instantiate with Quaternion.identity, and then in the next line of code, put this:
transform.Rotate(Random.Range(-5, 5), 0, 0, Space.Self);
This will rotate the instantiated object around the x axis, but not around the y and 7 axis. Just an example.