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 /
avatar image
0
Question by MMcCormack · Sep 30, 2016 at 06:03 PM · c#mecanimbeginner

Using Mecanim Trigger with same Key

Hi,

I'm trying to get a very simple Door opening/closing with Mecanim. I've done this other ways before but I wanted to try get using Mecanim more.

What I'm trying to do seems so simple to me but I really can't figure it out at all, I've looked through previous answers and googled several different tutorials but they all seem to do the same thing, which is open a door with a key press, then the door closes one they exit the trigger area.

However, what I want to do it open the door with "E" for example. Then when the mecanim triggers to open the door, and the door is now "open", the next time I press "E" it will close it.

I have 3 animations; Idle, Open, Closed. I have 2 triggers in Mecanim; Open, Close.

So what I want to do is the first time I press "E", i set the Open trigger. Then the 2nd time I press E its sets the Close trigger..

Here's the code if that helps people see exactly what I mean:

public class OpenDoor : MonoBehaviour { Animator anim; bool inRange = false;

 void Start()
 {
     anim = GetComponent<Animator>();
 }

 void Update()
 {
     if (inRange)
     {
         if (Input.GetKeyDown(KeyCode.E))
         {
             anim.SetTrigger("Open");
         }

         // This is where I'm having trouble.. 
         // I have another anim.SetTrigger("Close") that I also want to
         // use with the E input, but I can't figure out how to use it..
     }
 }

 void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.tag == "Player")
     {
         inRange = true;
     }
 }
 void OnTriggerExit(Collider other)
 {
     if (other.gameObject.tag == "Player")
     {
         inRange = false;
     }
 }

}

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

1 Reply

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

Answer by seckincengiz · Sep 30, 2016 at 06:38 PM

Make a simple switch to check door's current state and set parameter according to it.

 void Update()
      {
          if (inRange)
          {
              if (Input.GetKeyDown(KeyCode.E) && door)
              {
                  anim.SetTrigger("Open");
                  door = !door;
              }
              
              if (Input.GetKeyDown(KeyCode.E) && !door)
              {
                  anim.SetTrigger("Close");
                  door = !door;
              }
     
              
          }
      }


Comment
Add comment · Show 5 · 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 MMcCormack · Sep 30, 2016 at 07:47 PM 0
Share

Hi, thanks for the quick response! So in what you've written would !door not always be the case? As in, both events set door = !door at the end?

Also since this is a beginner question, where/what would i put the door variable as? A simple bool, or do you mean I need to actually make a switch statement?

Sorry for such basic questions, I've been away from Unity for the last few months and it's surprising how much I've forgotten...

avatar image MMcCormack · Sep 30, 2016 at 08:12 PM 0
Share

Ah ok! I've added a bool for the door, however using what you suggested made it flip between the open/close animation instantly, so the door opened and then closed again with only 1 key press. I'm not sure why it was doing this but i tried encapsulating the if(Input**) into their own if statements so it checked it the bool was true first, then entered the next if statement and it's working exactly how I wanted now. Thanks a lot man for pointing me the right way! :)

Here's the code I ended up muddling together if anyone is interested (feel free to point any bad habits you see in there as I'm still very much a beginner at coding).

using UnityEngine; using System.Collections;

public class OpenDoor : $$anonymous$$onoBehaviour { Animator anim; bool inRange = false; bool door;

 void Start()
 {
     anim = GetComponent<Animator>();
     door = true;
 }

 void Update()
 {
     if (inRange)
     {
         if (door)
         {
             if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E))
             {
                 anim.SetTrigger("Open");
                 door = false;
             }
         }
         else if (!door)
         {
             if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E))
             {
                 anim.SetTrigger("Close");
                 door = true;
             }
         }
     }
 }

 void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.tag == "Player")
     {
         inRange = true;
     }
 }
 void OnTriggerExit(Collider other)
 {
     if (other.gameObject.tag == "Player")
     {
         inRange = false;
     }
 }

}

avatar image seckincengiz MMcCormack · Sep 30, 2016 at 08:16 PM 0
Share

I'm happy that you solved your problem. And sorry for incomplete answer. Hope it didn't take your time too much.

avatar image Bonfire-Boy MMcCormack · Sep 30, 2016 at 08:26 PM 1
Share

The reason it was doing that is that whenever the first if block was entered, it changed the value of door so that the second if block was entered as well. Simply adding an else before the second if would have prevented this (which is basically what you've done).

In reference to your other comment, door = !door toggles the value of the variable, so it doesn't always set it to the same thing as you were suggesting.

avatar image MMcCormack Bonfire-Boy · Sep 30, 2016 at 08:49 PM 0
Share

ah I see. Thanks for clarifying Bonfire-Boy!

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

7 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Access Mecanim/Humanoid Muscles 0 Answers

How would I make a ListChangedEvent? 1 Answer

How to get root motion relative (not absolute) 1 Answer


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