Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 CrisisMode · Jun 24, 2014 at 05:54 PM · animation2dtrigger

Simple 2D animation trigger

Hello,

I wished to have a script that I could place on 2d box colliders that I have set to triggers. I had the idea that I could attach this script and have it play the animation of whichever public transform I have selected. So I wrote the following script:

 using UnityEngine;
 using System.Collections;
 
 public class AnimationTrigger : MonoBehaviour {
 
     public Transform animatedObject;
 
     private CharacterController2D _controller;
 
     public void Awake(){
 
         _controller = GetComponent<CharacterController2D> ();
 
         }
 
     public void OnTriggerEnter2D(Collider2D col){
 
         Debug.Log ("Player has triggered the animation!");
 
         if (_controller != null)
             animatedObject.animation.Play();
 
         gameObject.SetActive (false);
 
     }
 }

It allows me to place the transform in the right place, and the collider recognizes that the player has entered it according to the console. But the animation does not play and I feel that I missed perhaps a pretty small thing in my code.

alt text

This is what I am working with. The piece of ground has a 'fall' animation, but I removed the animator component from it. Underneath of course is the box collider with the script on it. And the player to the left. Any assistance with what I may have missed would be greatly appreciated. Thanks!

gameview.png (78.1 kB)
Comment
Add comment · 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 CrisisMode · Jun 24, 2014 at 10:50 PM 0
Share

I really wish I knew what was going on, it seems like such a simple task and it's been driving me nuts all day.

avatar image meat5000 ♦ · Jun 24, 2014 at 10:54 PM 0
Share

I think first decide if you are using $$anonymous$$ecanim or Legacy. Try not to mix the two components Animator and Animation.

On the object you wish to Animate, in the script which contains the Animation code (do it here as it's already set up here), write a few functions which trigger the animations you wish to play.

Invoke these functions using GetComponent from the Remote object, ins$$anonymous$$d of trying to fire the animation directly.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by tebandesade · Jun 24, 2014 at 07:16 PM

Well you can always try to put some conditions and parameters in the animator such as float, bool,or trigger. In this case I recommend the trigger parameter and name it "x". Afterwards, you will have to write in the code animatedObject.animation.settrigger("x"); . This means that when something happens( The character enters, the trigger "x" will be on) Yet, you need to set the animation in the animator with a condition to say that when trigger, the animation plays. Therefore, when "x" is trigger, the animation will play.

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 CrisisMode · Jun 24, 2014 at 08:06 PM 0
Share

Neither of those worked.

avatar image
0

Answer by SteelArrow21 · Jun 24, 2014 at 07:18 PM

OK, this is what I personally would've done:

  1. Attach this script to the trigger (You'll get errors at first, but they'll disappear soon):

               using UnityEngine;
             using System.Collections;
              
             public class AnimationTrigger : MonoBehaviour {
    
                 //In the inspector, make this the floating piece of ground
                 public GameObject floatingGround;
                 //Make this the player
                 public GameObject player;
             
                 void OnTriggerEnter2D (Collider2D other) {
             
                     if (other == player) {
                         floatingGround.GetComponent<GroundFall>().makeFall = true;
                     }
                 }
             }
    
    
    
  2. Attach this script to the piece of floating ground:

               using UnityEngine;
             using System.Collections;
                 
             public class GroundFall : MonoBehaviour {
                     
                 [HideInInspector]
                 public bool makeFall = false;
                 
                 void Update () {
                     if(makeFall == true) {
                         Animator animator = GetComponent<Animator>() as                Animator;
                         animator.SetTrigger("Fall");
                     }
                 }
             }
    
    
    
  3. Give the floating piece of ground an Animation Controller.

  4. Give the floating piece of ground the Animator component and set the Animation Controller as the Animator's Animation Controller.

  5. Drag the fall animation onto the Animation Controller Tab.

  6. Add a parameter called "Fall" and make it a trigger.

  7. Add a transition from AnyState to the fall animation and set the condition to "Fall"

  8. Please, I insist that you let me know if anything about this is wrong because I will be more than happy to help fix it because I'm kind of just going off what's in my mind right now.

Also, these scripts are universal meaning they'll work for any trigger and object just as long as you follow the steps. Hope this helps!

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 SteelArrow21 · Jun 24, 2014 at 07:29 PM 0
Share

Sorry the code is positioned a little far to the right. I was having trouble getting it to work and this is the only way I could.

avatar image CrisisMode · Jun 24, 2014 at 09:05 PM 0
Share

This didn't work.

avatar image CrftyZombiee · Sep 02, 2015 at 06:19 PM 0
Share

Hi,

I know this thread is old but I wanted to contribute as I have a similar situation where I have a GameObject with an animation that's the child of my camera that I am trying to trigger via a box collider2D. Anyways, I attached the first script to my trigger and the second to my GameObject. The GameObject has an Animator Component added to it which has the Animator Controller with the ‘Fall’ trigger parameter.

I then selected my trigger within the Hierarchy and dragged the GameObject to the Floating Ground spot and my character GameObject to the ‘Player’ GameObect spot.

When I hit the trigger, the GameOject I attached the second script to, doesn’t play the animation. I added a debug.log after the if statement in the first script and the debug text is displayed within the console. So why is my “other == player” not true? Any advice on how to get this to work would be greatly appreciated.

avatar image CrftyZombiee · Sep 02, 2015 at 07:30 PM 0
Share

Actually I figured it out I changed the following:

if (other == player)

to if (other.CompareTag("Player"))

as my character has a player tag added to it.

However now my animation continues looping when I enter the trigger and I only need it to play once.

Thanks for helping me get this far!

avatar image KUFgoddess CrftyZombiee · Mar 23, 2017 at 12:08 PM 0
Share

remember to turn loop pose off on the animation clip.

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

6 People are following this question.

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

Related Questions

OnTriggerStay2D Breaks When Adding AnimationController 1 Answer

Animation on public transform from script 2D 0 Answers

2D Animation does not start 1 Answer

Play 2D animations via trigger parameters 1 Answer

How to handle animation of multiple Coins? 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