Help with bools and if else statements. Animations
Hello guys, i have a animaton i want to play when i left click. The animation is to open and close a door. I went about it like this tell me of any errors you see and if you can help me with my issue.
~What i want to happen: What i want to happen is for it to switch bools to when i click a second time the door will close but the door only opens at the moment
using UnityEngine;
using System.Collections;
public class Door : MonoBehaviour {
Animator anim;
bool Open = true;
bool close = false;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.Mouse0) && Open == true)
{
anim.SetTrigger("Open");
Open = false;
close = true;
}
if(Input.GetKeyDown(KeyCode.Mouse0) && close == true)
{
anim.SetTrigger("Close");
close = false;
Open = true;
}
}
}
Any questions message me with them and i can explain more. Any help would be so nice! Thanks in advance!
Answer by PictonicStudio · Oct 23, 2015 at 04:55 AM
You are making this needlessly complicated.
You could either continue what you are doing with the current setup, and do this:
if (Input.GetMouseButtonDown(0))
{
Open = !Open;
}
if (Open)
{
anim.SetTrigger("Open");
}
else
{
anim.SetTrigger("Close");
}
Or what I would recommend is to redo your door config in the Animator to look something like this.
Then create a bool parameter called open.
Click on the following transitions and change the bool parameters as follows.
Then the code for that would be as simple as:
if (Input.GetMouseButtonDown(0))
{
Open = !Open;
anim.SetBool("Open", Open);
}
Oh and also, you need to look into Animator Triggers to find out what they are meant for.
Hey man thanks for the help. It helped me soo much. Sorry to ask but i was wondering if i could make it so when i stand in a triggerzone and press the mousebutton and then and only if those 2 requrements are met the door will open and close when pressing the button. Thanks again for all the great help!!
Sure! Just create a collider on your zone, set it as a trigger, and give it a tag.
public Animator anim;
private bool open;
private bool inZone = false;
void Update()
{
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
if (inZone)
{
open = !open;
anim.SetBool("Open", open);
}
}
}
EDIT: //Set $$anonymous$$yZone to your player's tag
void OnTriggerExit(Collider Other)
{
if (Other.CompareTag("PlayerTag"))
{
inZone = false;
}
}
void OnTriggerEnter(Collider Other)
{
if (Other.CompareTag("PlayerTag"))
{
inZone = true;
}
}
So i made a trigger with a tag called "Door" but now the door seems not not open at all. It does int matter if i'm in the trigger or not. Here is all my code
using UnityEngine;
using System.Collections;
public class Door : $$anonymous$$onoBehaviour {
public Animator anim;
private bool open;
private bool inZone = false;
void Update()
{
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
if (inZone)
{
open = !open;
anim.SetBool("Open", open);
}
}
}
//Set $$anonymous$$yZone to your trigger's tag
void OnTriggerExit(Collider Other)
{
if (Other.CompareTag("Door"))
{
inZone = false;
}
}
void OnTriggerEnter(Collider Other)
{
if (Other.CompareTag("Door"))
{
inZone = true;
}
}
}
Am i doing anything wrong or?
Very nice answer. I don't dabble with animations but I managed to learn something too! :) +1
Answer by seth_slax · Oct 23, 2015 at 04:42 AM
I'm not sure what your problem is since you didn't specify, but from what I can see, the boolean 'Open' should be 'open'. I'm not sure if it's necessary, but from what I've learned you can never start a variable with a capital, since that's reserved for classes, functions and whatnot. Also, some shorthand tips:
if(bool == true){}
//Is the same as:
if(bool){}
//And:
if(bool == false){}
//Is the same as:
if(!bool){}
//You can also switch a boolean by using:
bool = !bool
You CAN start a variable with a capital letter, it's just not recommended because methods are capitalized, and it's easier to keep track of which is which.
Your answer
Follow this Question
Related Questions
How do I make a gun reload after 30 shots? 4 Answers
Can´t find the infinite loop in this script 0 Answers
Changing eye texture using bools 0 Answers
JS script for certain actions when mouse is pressed 0 Answers
Get Time A Bool Has Been True 4 Answers