• 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 /
This question was closed Mar 10, 2015 at 10:02 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
Question by Ouija · Mar 09, 2015 at 08:48 AM · animationcollidercharacterontriggerexitontriggerstay

OnTriggerStay instead of input

Is there a way I can change the "if(Input.GetMouseButton(0))" to, onTriggerStay? Basically I want one animation to play on triggerstay, and onTriggerExit I want the second animation to play. Ex: A char walking into a sphere collider for the trigger? Not exactly sure if a player/char should be called somewhere

     void Update () 
     {
         if(Input.GetMouseButton(0))
         {
             Ray ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
             RaycastHit rayCastHit;
             
             if(Physics.Raycast(ray.origin, ray.direction, out rayCastHit, Mathf.Infinity))
             {
                 Monster monster = rayCastHit.transform.GetComponent<Monster>();
                 if(monster)
                 {
                     monster.PlayMonsterAnim();
                 }
             }
         }
     }


T$$anonymous$$s the anim.script

     private int m_LastIndex;
 
     public void PlayMonsterAnim () 
     {
         if(!animation.isPlaying)
         {
             if(m_LastIndex == 0)
             {    
                 animation.Play("moveOut");
                 m_LastIndex = 1;    
             }
 
             else 
             {
                 animation.Play("moveIn");
                 m_LastIndex = 0;
             }
         }
     }
Comment

People who like this

0 Show 2
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 MagicZelda · Mar 09, 2015 at 09:20 AM 0
Share

I would suggest moving your code to the collider events

void OnTriggerStay(Collider other) {} void OnTriggerExit(Collider other) {}

avatar image Ouija · Mar 09, 2015 at 09:29 AM 0
Share

I'm not exactly sure what you mean , could you explain a lil more? or help with example

2 Replies

  • Sort: 
avatar image
Best Answer

Answer by Mmmpies · Mar 09, 2015 at 10:19 AM

OnTriggerStay is an overhead all you need to do is set a bool with OnTriggerEnter to True and OnTriggerExit to False.

 private bool IveBeenTriggered = false;
 
 void OnTriggerEnter(Collider other)
 {
     IveBeenTriggered = true;
 }
 
 void OnTriggerExit(Collider other)
 {
     IveBeenTriggered = false;
 }
 
 // Then where you want your animation to play:
 
 if(IveBeenTriggered && !animation.isPlaying("moveIn"))
 {
     animation.Play("moveIn");
 }
 else if (!animation.isPlaying("moveOut")
 {
     animation.Play("moveOut");
 }

Although it's not really clear what triggers the animation currently anyt$$anonymous$$ng that enters that collider will.

Comment
Ouija

People who like this

1 Show 18 · 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 Ouija · Mar 09, 2015 at 06:57 PM 0
Share

Hey man sorry for the late reply, had to sleep lol. I am wondering where the animation should be called. Update perhaps? i asked because the "monster" isnt activated until you click on another game object. And I want the monster to pop out when the player walks near. Would using update be proper? or will that be using unneeded resources?

avatar image Ouija · Mar 09, 2015 at 07:07 PM 0
Share

im getting the error, " Unexpected symbol - line 20" when I put it in update, so, i guess not hah

avatar image Mmmpies · Mar 09, 2015 at 07:10 PM 0
Share

Kinda depends on what you're doing with the animation but if you just want the moveIn animation to play once when OnTriggerEnters you could just put it in the trigger and put the moveOut in the OnTriggerExit. No need for the bool then.

Or, if you want to check every frame you could put it in Update if that's easier and check that bool.

It's Unity so there's always 10's if not 100's of ways of doing things.

Chances are just playing the animation in the OnTriggerEnter/Exit will be good enough but depends on animation looping and what you want to do when the animation ends for either one.

avatar image Mmmpies · Mar 09, 2015 at 08:24 PM 1
Share

Glad it helped @Ouija and no problem, still learning loads of stuff on here myself so maybe 1/2 a year to a year ahead of you. Gets easier doing simple stuff but man it's a big subject.

avatar image Mmmpies · Mar 10, 2015 at 06:06 PM 1
Share

OK, sorry for the late response and my next might not be too fast either, but are you setting this up to use the animation to move the character out and back in?

It can be done that way so you effectively end up with a cuckoo clock type effect, say you had a cube in a cave that when the trigger Enters animates out and when the trigger exits goes back in.

But, if you are using animations like this, there are better ways. I'll see if I can setup a test scene later with some free assets to show you a simple State Machine but you really want to gen up on Mecanim as it's ideal for easy state machines.

Show more comments
avatar image

Answer by kevinspawner · Mar 09, 2015 at 09:20 AM

You can create a triggerstay and trigger exit function and use bool to switch between the animation in the trigger function.

Comment
Ouija

People who like this

1 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 Ouija · Mar 09, 2015 at 09:43 AM 0
Share

I wrote this, is it close to what you were talking about? if so, i'm still unsure how to set up the ontriggerstay /exit tho lol

  public void PlayMonsterAnim (bool something) 
      {
  
          if(something) {
  
          if(!animation.isPlaying)
          {
              if(m_LastIndex == 0)
              {    
                  animation.Play("moveIn");
                  m_LastIndex = 1;    
              }
  
              else 
              {
                  animation.Play("moveOut");
                  m_LastIndex = 0;
              }
          }
      }
  }

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Trigger animation going crazy 3 Answers

Simple AI and Animation 2 Answers

How to use OnTriggerEnter with multiple triggered objects? 1 Answer

OnTriggerExit is not calling 2 Answers

Audio not playing after OnTriggerStay is applied to the scripts (Driving Simulator Project) 0 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