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 oliverpt · Feb 17, 2016 at 12:39 PM · animationanimatoranimator controlleranimationclip

How to play an Animation only once from my MachineState Animator?

Hi, I created a MachineState Animator with blendtrees, and many animations. Many of these animations has a input (GetButtonDown). For example, I put the animation "Jump" with the input "space". What happens is that when I press a lot of times the "space" button the animations goes to the start point and my character literally flies in the field. What I need is when I press "space" the "Jump" animation must go to the end, even when I press the same button many times.

My code is this:

 using UnityEngine;
 using System.Collections;
 
 public class TestMotion : MonoBehaviour {
 
     private Animator myAnimator;
 
     // Use this for initialization
     void Start () {
 
         myAnimator = GetComponent<Animator>();
     
     }
     
     // Update is called once per frame
     void Update () {
 
         myAnimator.SetFloat ("VSpeed", Input.GetAxis ("Vertical"));
         myAnimator.SetFloat ("HSpeed", Input.GetAxis ("Horizontal"));
 
 
         if (Input.GetButtonDown ("Jump")) { 
             
             myAnimator.SetBool ("Jumping", true);
             Invoke ("StopJumping", 0.08f);
         }
 void StopJumping() {
             myAnimator.SetBool("Jumping", 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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by coolraiman · Feb 17, 2016 at 01:41 PM

your animator is a wonderfull state machine, use its power

  if (Input.GetButtonDown ("Jump") && !(myAnimator.GetCurrentAnimatorStateInfo(0).IsName("Jump"))) 
 {
 ... 
 }

also, you may want to use triggers instead of bool when a trigger is activated and then used to fill a condition to start an animation in your animator, it is turned off it allow to use much less hard coding

Comment
Add comment · Show 4 · 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 oliverpt · Feb 17, 2016 at 04:14 PM 0
Share

Thank you coolraiman for your answer. I agree with you, my question its more a question about preventing the player from jumping while being in a jump animation. I tried your suggestion with your if condition, but something is missing me:

 if (Input.GetButtonDown ("Jump") && !(myAnimator.GetCurrentAnimatorStateInfo(0).IsName("Jump"))) 
         {
             myAnimator.SetBool ("Jumping", true);
             Invoke ("StopJumping", 0.08f);
         }

I put this code but didn't work. The code has no errors, the animator runs nicely, but the problem persist. Can you help me? I also unchecked the loop time in the inspector.

avatar image meat5000 ♦ oliverpt · Feb 17, 2016 at 04:19 PM 0
Share

The (0) is the Layer Index. Change it to whatever layer your Jump anim is on.

avatar image oliverpt meat5000 ♦ · Feb 17, 2016 at 06:09 PM 0
Share

I only have the Base Layer, so the "0" is correct? Where are the information about the number of the layers?

Show more comments
avatar image
0

Answer by Guhanesh · Feb 17, 2016 at 12:56 PM

alt text

Uncheck Loop Time in the inspector of your Animation files.


screenshot-2.jpg (19.5 kB)
Comment
Add comment · Show 2 · 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 coolraiman · Feb 17, 2016 at 02:05 PM 0
Share

its more a question about preventing the player from jumping while being in a jump animation

avatar image oliverpt · Feb 17, 2016 at 04:20 PM 0
Share

Thx Guhanesh, my animations has the loop time unchecked, but it still has this problem.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Animator Override Controller changed at runtime doesn't always play the animations correctly 1 Answer

Why does my transition freeze the second animation on it's first frame? 1 Answer

Dynamically create states and transition using Animator Controller 1 Answer

Is there a way to change the WrapMode of all Animations in a Mecanim Animator? 1 Answer

Animation clip 'BackgroundAnim' is not retargetable? 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