- Home /
Need to activate and deactivate objects really fast, doesn't work
Hello,
In order to do a muzzle flash effect for my FPS, I need to activate a parent gameobject that contains two muzzle textures and a spotlight and deactivate it just after in a very short period of time. So that it gives this "flash" effect.
The problem is that sometimes when I fire no muzzleflash appear, or only one of the two, etc. It's like the period of time is too short for the engine to activate all it has to.
So I don't know how to do this muzzleflash effect. The only solution I found is too increase the period of time but when the muzzle stays 0.5s at the screen it's way too long...
Here is my activation/deactivation code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WeaponRecoil : MonoBehaviour {
public float muzzleDuration = 0.1f;
public GameObject muzzFlashes;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetMouseButtonDown(0)) {
muzzFlashes.SetActive(true);
StartCoroutine(deactivateMuzzle());
}
}
IEnumerator deactivateMuzzle()
{
yield return new WaitForSeconds(muzzleDuration);
muzzFlashes.SetActive(false);
}
}
Thank you.
Just try doing animation and set it as trigger. While animating you will be able to set values as you want and speed how fast to animate. It doesnt work since you are using WaitForSeconds and 0.1f second is just too little to make it work. You can also test with WaitForEndOfFrame
Thank you for your answer. I just tried to animate and that seems a wonderful idea. Thanks !!
Glad to help. If it really helped you in this situation then mark my answer as correct please so people looking for similar problems find their answers.
Answer by Vega4Life · Dec 27, 2018 at 02:20 PM
I think disabling the particle isn't the right way to handle this situation (or any particle for the most part). One of the reasons why is what you are encountering now - trying to time it correctly with an outside timer. This goes against the particle having its own timers, etc. The other reason is because if you just disable a particle, its an instant stop. Most times this looks really silly. Imagine disabling a fire particle - it won't look realistic. In those cases you would want to disable the emissions so it lets the current alive particles finish
I have a game that uses a muzzle flash also. It isn't tied with any outside timers or fire rates of my weapons. The most important thing is I just reset and play the particle when the weapon is fire. Using below:
// This is called every time I shoot
public void EnableFX(bool enable)
{
if (enable)
{
particle.Clear(); // This essentially resets the particle
particle.Play(); // This plays particle
}
}
You just need a reference to the particle and the above works. You don't need a timer or anything like that. The particle will always play, but can interrupt itself (which is fine if a weapon is firing super fast). I'll also post an image of my particle settings. The important thing on it is the simulation speed. Mine is set to 2, so that I get a super fast particle. Hope this info helps, good luck.
Your answer
Follow this Question
Related Questions
one button to hide and unihed gameobjects 0 Answers
GameObjects that I deactivate are reactivated immediately 0 Answers
c# Set Active error, conflicting with other code.. 1 Answer
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Panel GameObject not activating 0 Answers