Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 PancakesNBacon · Jul 08, 2015 at 06:35 PM · animation2d gameanimatortrigger

Starting a 2D animation on a trigger

Making a game where you play as a character made of fire. I want to make it so when the player jumps into the torch, the torch lights and the fire animation appears and plays. Here is what I have coded so far, I just can't figure out how to only start playing the animation once torchIsLit = true

 public class lightTorch : MonoBehaviour {
 
     private bool torchIsLit = false;
 
     Animator anim;
 
     // Use this for initialization
     void Start () {
         anim = GetComponent<Animator> ();
     }
     
     // Update is called once per frame
     void Update () {
 
         if (torchIsLit) {
             Debug.Log ("Torch Lit");
         }
     }
 
     void OnTriggerEnter2D(Collider2D other) {
         torchIsLit = true;
     }
 
 }
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 Hellium · Jul 08, 2015 at 07:16 PM

Why don't you simply play the animation inside the OnTriggerEnter2D ?

Depending on the animator (state machine) you have created, you may want to play the animation using a trigger.

In your animator :

 [`<newEmptyState>`] ---→ [`<nameOfYourAnimation>`]

with the transition triggered by a trigger parameter.

Then, you call anim.SetTrigger(<nameOfYourTrigger>);

Comment
Add comment · Show 6 · 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 PancakesNBacon · Jul 09, 2015 at 06:04 PM 0
Share

Alright so I did this, and the trigger is working, setting torchIsLit to true, but the animation is playing and on the screen before I even collide with it. I want the torch to have nothing in it until I run into it and then it lights, making the animation start and now visible. Am I doing something wrong?

 public class lightTorch : $$anonymous$$onoBehaviour {
 
     //private bool torchIsLit = false;
 
     Animator anim;
 
     // Use this for initialization
     void Start () {
         anim = GetComponent<Animator> ();
     }
     
     // Update is called once per frame
     void Update () {
 
     }
 
     void OnTriggerEnter2D(Collider2D other) {
         //torchIsLit = true;
         anim.SetTrigger ("torchIsLit");
         Debug.Log ("Torch Lit");
     }
 
 }
 
avatar image Hellium · Jul 09, 2015 at 06:09 PM 0
Share

$$anonymous$$ake sure the animation is not the default state of the Animator (in orange).

If so, add an empty state (in the animator window : right click > Empty state) and link the Enter state to the empty state.

In fact, it would be better to do like this :

alt text

Sorry for poor quality drawing

animator.png (3.9 kB)
avatar image PancakesNBacon · Jul 09, 2015 at 06:20 PM 0
Share

Update

I got the animation to appear and start once I collide with it now, but when I do the animation breaks. In the animator the bar repeats over and over without completing the animation so it basically stays on one frame and doesn't animate at all. Why would this happen?

avatar image Hellium · Jul 09, 2015 at 06:27 PM 0
Share

Check the conditions of your transitions :

  • Idle → Light : Disable exit time, add the trigger parameter

  • Light → Idle : Enable exit time, no parameter

avatar image PancakesNBacon · Jul 09, 2015 at 06:57 PM 0
Share

Thank you! It is working now. Before I had "Any State" -> "Light" ins$$anonymous$$d of "Empty State" -> "Light". Switching it fixed the problem, not exactly sure why, but we are golden now :D

Show more comments

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

22 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

Related Questions

Shooter 2D: Character animation only runs once 1 Answer

Smooth transition of position between animations 0 Answers

On/Off Animator using script 2 Answers

How check if an animation trigger is playing? 1 Answer

AnimationEvent triggers twice 3 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