Set Bool to false after seconds?
Hi! I'm quite new to Unity, (well in my books, a few months + no training = new lol) therefore I'm not the best at coding, and I was wondering if I could get a few pointers. I have an animation for my player to raise a gun on cue (I can't get it back down, I have a bool parameter so that when I hit space the bool = true, and I couldn't get it back down again it kept giving me compiling errors) Also I've been trying to get the actual gunshot animation to work (I have it with another bool so when you hit "z", Bang = true but the issue is that it stays true (leaving you frozen with no animation) and I'm thinking I could use a timer so it becomes false before the animation is over so it just goes back to the idle animation, but I couldn't get that working either. I eventually got bored and focused more on art (I have glow effects and lens flares + my art it looks AWESOME) but you cant do much other than take out your gun and shoot once (But then you're stuck, frozen). I also had some dialogue working, but I must've messed up colliders and now the whole thing is just a mess with sitting code that doesn't do anything lol. No compiling errors (That make it unplayable anyway) and looks gorgeous. Anyway if someone could help me out in any small way I would be extremely grateful as this is my ultimate dream (not to be cheesy) but I've always wanted to do this and this is my first game. :)
Answer by Carterryan1990 · Feb 02, 2018 at 01:50 AM
bool bang;
public float m_TimeUntilBang;
void Start()
{
Invoke("Onbang", m_TimeUntilBang);
}
void OnBang()
{
bang = !bang;
//Or it could be
bang = false;
}
Void YOURMETHOD()
{
Invoke("Onbang", m_TimeUntilBang);
}
}
I rather use invoke its easier to set up and to me a more convenient way to use a timer.
bool bang; public float m_TimeUntilBang;
void OnBang() { bang = !bang; //Or it could be bang = false; } Void YOURMETHOD() { Invoke("Onbang", m_TimeUntilBang); } }
you could also call your method in the start function. so
void Start() { YOURMETHOD(); }
I rather use invoke its easier to set up and to me a more convenient way to use a timer.
@Carterryan1990 Hi, this is my second time commenting, wifi screwed up last time lol. I don't know if it will post the other one eventually, but basically I said "It looks like it makes sense, but where would you write how long you have to wait?" and thanks for the reply btw ^^
@Noodles_yummy No problem. The public float m_TimeUntilBang, Is your value. After creating the script and placing it on a scene object you will see in the inspector a public variable called m_TimeUntilBang with a text box to the right of it, where you would input a number. You could also do it like this, float m_TimeUntilBang = 3. Which would automatically set the float to 3.
When you start your scene if the method is called on start your bool would be called in 3 seconds. As you see in the script, i use Invoke("THE$$anonymous$$ETHOD", and then the value which is m_TimeUntilBang, which = 3. Invoke will call the method after real time has past your value. Say you set the value to 60, it would take 60 seconds until the method is called.
Can i ask what exactly you are trying to do and ill write the script for you and explain to you how i did it. thanks!
hmm... this wont work?.. $$anonymous$$Y bool is still true..
Answer by Nighfox · Feb 01, 2018 at 04:14 AM
You might want to post some of your animation code and your Animator controller setup. This might be either a logic issue or a transition problem.
@Nighfox Heya! Thanks for the reply, here is my animator and a few lines of code, hope this makes sense lol. Like I said I'm pretty new to this.
Hmm..it's not letting me post the third picture so I just copied and pasted my code! This will look extra messy, sorry about. //me trying things out, like I said it's pretty messy lol. This wouldn't work cause gunpoint was already true. if (gunpoint = true) if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.R)) anim.SetBool("gundown", true);
//just movement, it works just fine
Vector2 movement = new Vector2 (moveHorizontal, moveVertical);
//ignore this, just movement some more
rb2d.AddForce (movement * speed);
}
//I took some stuff off the internet and tried to get it working, I don't know why it's not, I'm not getting any compiling errors... hmm...
void Foo() { StartCoroutine(Begin()); }
IEnumerator Begin()
{
if (BANG = true)
yield return new WaitForSeconds(3f);
BANG = false;
// Code here will be executed after 3 secs
//Do stuff here
Thanks again!
What' s calling Foo() ? Also, it's worth noting that regardless of your value for BANG
:
if (BANG = true)
{
// something
}
will always run. That line sets BANG
to true and then the if
statement evaluates that line as true. I think you want:
if (BANG == true)
{
// something
}
@TreyH Hey! Thanks for the reply! I think foo is just something the people who made the code have specific to their script and I just accidentally kept it in there lol. And I'll try that out, but what's the difference between = and ==? Thanks again ^^
Your answer
Follow this Question
Related Questions
Clarifaction needed on LoadLevel delay. 1 Answer
help for C# ; when all were green 1 Answer
How do I read this? private Rigidbody rb; 2 Answers
How To Add Animations Through C# Scripts 0 Answers