How do I animate a muzzle flash for a gun shot?
It is extremely complicated with the Animator window or the new upgraded functions in the later version of Unity. Every method for script is “deprecated”. You are making it harder and harder.
My personal intuitive way to do it:
I created a simple plan, position it in front of the gun, I added component “Animator”(not animation!) to the plan.
I open: Main menu- Window - Animation window.
Inside Animation window select: Add Property - Mesh Renderer - Enabled
Double click inside track to add keyframes. I added 2 Keyframes! #1 is enabled from mesh rendere checkbox, #2 is disabled. When I move the red line to play the frames, the muzzleflash stay open, and in the end is off.
Now, what to do?
void Update()
{
if (shoot)
{
animator.PlayInFixedTime(“muzzleFlash”);
}
And nothing!
I show you how I did it and is NOT WORKING!!!
How can I make it work?
A simple muzleflash?
Not a human animation, or a tank animation, just a simple little tiny muzleflash.
This was a awesome frustrating experience.
Thank you.
It sounds like the flash is not an animated sprite or an object that deforms so I wouldn’t use any animation on it at all. What I would do is have a transform on the end of your gun muzzle, a public transform in your gun script, a public game object in your gun script and a prefab of your muzzle flash object.
In your gun script you will want to assign the transform of the gun muzzle to the public transform, assign the muzzle flash object to the game object and then in your script you will want to trigger the creation of the muzzle flash when the shoot button is pressed. Then I would have a script on the muzzle flash object itself that has a timed destruction in update.
That’s certainly not the only way to do it, that’s just how I would.
You are right, the “flash” Its a simple *flash.PNG file, (having some alpha around the flame).
It is not animated like a gif. I dont have multiple images of its life - like a sprite.
— It is 1 single image. —
I want it appear and disappear(with some fading if possible - if not, its ok in brute mode).
I will test your answer, right now. Thanks.
im stuck on adding the muzzleflash texture to the [game object] parameter from the script_gun in inspector.
It seems I have “Imported” a texture , and NOT a game object.
//Thats why I made in the first place the plan and assign the texture into its material.
How to resolve the texture problem?
When I click on the muzzleflash icon in the [Project] window, The Inspector updates with these information:
<
muzzleflash Import settings
Texture type ___________ texture
etc…
(1 day passed.) I still don’t get it.
How do I animate a muzzle flash for a gun shot?
Anybody else? Or the same guy? All that I care is this thing to work and now it is not.
So what you will want to do is create a 2 sided plane… I’m assuming this is a 3D game but anyway. Create a plane not in unity because unity creates planes with a ton of unnecessary geometry. Just a simple square, 4 sided plane with be
sufficient, duplicate that, flip it around 180 so that there are two planes facing out (BTW two sides because unity always uses back face culling). Group those two items and export the group. Bring that object into unity and drop it in your scene. Texture the planes with a material in unity that has your flash texture. You should now have an object in your scene that looks like your muzzle flash on two sides. Drag and drop that object from your hierarchy into your project, that action creates a prefab. From there you can assign that prefab to your script.
Now that being said. And no offence intended but judging by your questions I am thinking that you are very new to this. I would highly recommend following some you tube tutorials to get familiar with this medium. The unity tutorials are a great place to start, but here is another resource:
Anything this guy does is golden:
or just straight to his youtube channel:
I’ll be glad to help, but you may want to bone up on some of this stuff before going any further.
I am new to unity - but not very new. My game is 3d, sorry not mentioning it.
I already have a plan with a texture on it that is visible from front and the back of the plane. This is not a problem.
My problem is how to animate it !
I already added a sound to the gun when is firing.
private new AudioSource audio;
void Start()
{
audio = GetComponent();
}
void Update()
{
if (shoot)
{
audio.PlayOneShot(shootSound, 1f); audio.volume = 0.3f;
//But i dont see the plan or the texture methods to do it like with the sound. It is Not intuitive!
}
}
Dont be mad on me, but your tutorials respond to a larger area than the simple question I made here. Can you find something more specific next time? Thx.
I will look on those tutorials too.
I followed this tutorial: Game Setup, Player and Camera - Unity Learn
But the code there is deprecated in the last version of unity to date 5.3.
Should I Downgrade my Unity version to that version from the video?
What is your suggestion?
----tutorial code-----
using UnityEngine;
using System.Collections;
public class Mover : MonoBehaviour
{
public float speed;
void Start ()
{
rigidbody.velocity = transform.forward * speed; // THIS CODE IS A LIE !!! AND IS FROM UNITY TUTORIAL PAGE!!!
//IN NEWER VERSION OF UNITY IT SAYS “DEPRECATED” !!! - VERY FRUSTRATING AND ANNOYING !!!
SIMPLY PUT, IS IMPOSSIBLE TO MAKE A SINGLE SPRITE FLY AROUND.
}
}
What the hell? Why can’t anybody give me a straight answer how to make a sprite fly in Unity 5.3 ?
DUuuuuDE, Thank you from my heart for the time and effort you took to make this wonderful tutorial!
BUT…
$$%^%$#&*^$$
Do you know how to make a tutorial? Simple, concise and to the point? I understand you probably are not a pro, but you can imitate one these days very easy - for example look on the official unity tutorials how are made. There is something you missed mentioning in this tutorial, and when I try to replicate your steps it didn’t work. Again, very easy I become frustrated and annoyed. Too many steps, my friend - too many. Make it clear like the water in the river Kay from the mountain Voo. Ok? Don’t be mad on me. I am a simple user (frustrated one).
My suggestion for You is this:
Make another tutorial from 1-2minutes long !!! (in my opinion this type of tutorial should not extend over 30s)
Short directions and to the point. (I really like your voice - it’s very soft and peaceful)
Check your code and check your steps. (Remake from scratch the entire project after your steps! - it shouldn’t take you more than 10min – if buggy)
I don’t know, use your good intuition if the end movie result is good enough.
Really, this is a marathon, not a sprint. If you choose to help around, learn to do it right. Ok? Still,not mad on me,right? Keep the good work, my friend.
I expect your answer.
Hey friend, thanks for the input, I appreciate it. But that is all I’m going to give this. I know it works because… well it works. As you say, I’m not a pro and I fully admit that. That was the first tutorial I have ever really made and they will get better, but I’m not looking for perfection. Just getting something out the door you know?
Good call from you to share your project - well done!!! -It helped!
Indeed, is a bit complicated to explain something in unity environment…
I understand your ‘hurry’, my friend.
Here is how to make a tutorial (q12 version):
[all tested and working]- no bugs I swear. Only if you follow every step and dont do anything around, you are safe from bugs.
I have open: Project(window),Hierarcy(window),Inspector(window) + Scene(window), Game(window).
Add 3d Gun Mesh (can be a simple cube too) (name it Gun) -it will act as a parent folder (1 parent)
create empty: Hierarcy- Create- Create Empty (name it MuzzleLocation) -as child
create a plane: Hierarcy- Create- 3d Object- Plane (name it MuzzleFlash) - (move it to Project)as prefab ! (delete from Hierarcy)
Add texture.png - as prefab !
create script: Project- Create- c# Script (name it script_Destroy_MuzzleFlash)
create script: Project- Create- c# Script (name it script_Initialize_MuzzleFlash)
In Project- Create- Material (name it Mat_MuzzleFlash)
select Mat_MuzzleFlash and in Inspector, add texture.png to the Albedo property.
(drag texture.png from Project window and drop into Albedo square)
select Mat_MuzzleFlash and in Inspector, from Shader- select Particles- Additive.
(makes visible 2 sides of the same plane + transparency)
SELECT [MuzzleFlash] (the plane prefab)
select MuzzleFlash and in Inspector, uncheck [Mesh Collider](or delete it) (it’s a flame, not a solid)
select MuzzleFlash and in Inspector - Transform- Scale x=0.3,y=1,Z=0.3
Drag Mat_MuzzleFlash into inspector (drag into empty area)
Drag script_Destroy_MuzzleFlash into inspector(drag into empty area)
SELECT [MuzzleLocation] and in Inspector - Transform- Position x=0,y=1,Z!=3 (move it in Front of the 3d gun mesh/cube mesh)
SELECT [Gun]
Drag script_Initialize_MuzzleFlash into inspector(drag into empty area)
in Inspector- script_Initialize_MuzzleFlash
-drag [MuzzleFlash](the plane prefab) to muzzleFlash field
-drag [MuzzleLocation] to muzzleLocation field
Press Play
In Game(window) press ‘F’ key to fire.
(if the plane/texture location is misaligned, SELECT [MuzzleLocation] and reposition.)
In Scene(window) rotate,move the gun around, to see the effect. Only in Game window F key works!
using UnityEngine;
using System.Collections;
public class script_Initialize_MuzzleFlash : MonoBehaviour
{
public GameObject muzzleFlash;
public GameObject muzzleLocation;
public class script_Destroy_MuzzleFlash : MonoBehaviour
{
GameObject gun;
float timeToDestroy = .05f;
void Start()
{
gun = GameObject.Find(“Gun”);
//for some reason, the texture dont keep the default settings for global rotation
//now texture rotate with the gun rotation.
transform.rotation = Quaternion.Euler(90, gun.transform.rotation.eulerAngles.y, -90);//degree
Destroy(gameObject, timeToDestroy);
}
}
I have another question.
If instead of pressing the F key, the gun is automated and shoots at an interval of 1.5seconds,continuously?
How the code should look?
public class script_Repeat_MuzzleFlash : MonoBehaviour
{
public GameObject muzzleFlash;
public GameObject muzzleLocation;
public float time = 2;
public float repeatRate = 1f;
public bool repeat = true;
void Update()
{
if (repeat)
{
repeat = false;
CancelInvoke(“RepeatFlashing”);
InvokeRepeating(“RepeatFlashing”, time, repeatRate);
}
}