Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Creepercatss · Oct 23, 2015 at 04:14 AM · animationbooleanif-statements

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

alt text

Then create a bool parameter called open.

Click on the following transitions and change the bool parameters as follows. alt text

Then the code for that would be as simple as:

         if (Input.GetMouseButtonDown(0))
         {
             Open = !Open;
             anim.SetBool("Open", Open);
         }
         


door-anim-config.png (26.4 kB)
tut.jpg (123.7 kB)
Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PictonicStudio · Oct 23, 2015 at 05:01 AM 0
Share

Oh and also, you need to look into Animator Triggers to find out what they are meant for.

avatar image Creepercatss · Oct 23, 2015 at 05:43 AM 0
Share

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!!

avatar image PictonicStudio Creepercatss · Oct 23, 2015 at 06:27 AM 0
Share

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;
         }
     }
avatar image Creepercatss PictonicStudio · Oct 23, 2015 at 01:19 PM 0
Share

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?

Show more comments
Show more comments
avatar image Statement · Oct 24, 2015 at 09:54 PM 0
Share

Very nice answer. I don't dabble with animations but I managed to learn something too! :) +1

avatar image
-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
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PictonicStudio · Oct 23, 2015 at 04:57 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

42 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges