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 alexanderameye · Jan 08, 2014 at 03:54 PM · animationmecanimif-statementswhileloops

Mecanim animation keeps running infinite

I made a character controller with mecanim and I have an idlestate for idling, a locostate for walking and a runningstate for running. The transition from walking to running is a boolean called "Sprint". I want the character to run WHILE i press leftshift button on my keyboard, but now the character just keeps running when I pressed shift once. Thank you! This is my script:

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof (Animator))]
 [RequireComponent(typeof (CapsuleCollider))]
 [RequireComponent(typeof (Rigidbody))]
 
 public class ControlScript : MonoBehaviour
 {
     [System.NonSerialized]                    
     public float lookWeight;                                                    // the amount to transition when using head look
     
     public float animSpeed = 1.5f;                                                // a public setting for overall animator animation speed
     public float lookSmoother = 3f;                                                // a smoothing setting for camera motion
     
     private Animator anim;                                                        // a reference to the animator on the character
     private AnimatorStateInfo currentBaseState;                                    // a reference to the current state of the animator, used for base layer
     private AnimatorStateInfo layer2CurrentState;                                // a reference to the current state of the animator, used for layer 2
     private CapsuleCollider Col;                                                // a reference to the capsule collider of the character
 
     static int idleState = Animator.StringToHash("Base Layer.Idle");    
     static int locoState = Animator.StringToHash("Base Layer.Locomotion");        // these integers are references to our animator's states
     static int jumpState = Animator.StringToHash("Base Layer.Jump");            // and are used to check state for various actions to occur
     static int rollState = Animator.StringToHash("Base Layer.Roll");
     
     void Start()
     {
         // initialising reference variables
         anim = GetComponent<Animator>();                      
         Col = GetComponent<CapsuleCollider>();                
         if(anim.layerCount ==2)
             anim.SetLayerWeight(1, 1);
 
         rigidbody.mass = 60f;
         rigidbody.freezeRotation = true;
 
         Col.center = new Vector3(0, 1, 0);
         Col.radius = 0.3f;
         Col.height = 2f;
         gameObject.tag = "Player";
     }
     
     
     void Update()
     {
         float h = Input.GetAxis("Horizontal");                // setup h variable as our horizontal input axis
         float v = Input.GetAxis("Vertical");                // setup v variables as our vertical input axis
         anim.SetFloat("Speed", v);                            // set our animator's float parameter 'Speed' equal to the vertical input axis                
         anim.SetFloat("Direction", h);                         // set our animator's float parameter 'Direction' equal to the horizontal input axis        
         anim.speed = animSpeed;                                // set the speed of our animator to the public variable 'animSpeed'
         anim.SetLookAtWeight(lookWeight);                    // set the Look At Weight - amount to use look at IK vs using the head's animation
         currentBaseState = anim.GetCurrentAnimatorStateInfo(0);    // set our currentState variable to the current state of the Base Layer (0) of animation
         
         if(anim.layerCount == 2)        
             layer2CurrentState = anim.GetCurrentAnimatorStateInfo(1);    // set our layer2CurrentState variable to the current state of the second Layer (1) of animation
         
         
         //JUMPING
         //Idle to Jump
         if (currentBaseState.nameHash == idleState)
         {
             if(Input.GetButtonDown("Jump"))
             {
                 anim.SetBool("Jump", true);
             }
         }
         
         //Loco to Jump
         if (currentBaseState.nameHash == locoState)
         {
             if(Input.GetButtonDown("Jump"))
             {
                 anim.SetBool("Jump", true);
             }
         }
         
         //ROLLING
         //Idle to Roll
         if (currentBaseState.nameHash == idleState)
         {
             if(Input.GetKeyDown(KeyCode.LeftControl))
             {
                 anim.SetBool("Roll", true);
             }
         }
         
         //Loco to Roll
         if (currentBaseState.nameHash == locoState)
         {
             if(Input.GetKeyDown(KeyCode.LeftControl))
             {
                 anim.SetBool("Roll", true);
             }
         }
 
         //AVOID REPEATING
         //jumping
         if (currentBaseState.nameHash == jumpState)
         {
             anim.SetBool("Jump", false);
         }
 
         //rolling
         if(currentBaseState.nameHash == rollState)
         {
             anim.SetBool("Roll", false);
         }
 
         if (currentBaseState.nameHash == locoState)
         {
             if(Input.GetKey(KeyCode.LeftShift))
             {
                 anim.SetBool("Sprint", true);
             }
 
             else
             {
                 anim.SetBool("Sprint", false);
             }
         }
 
         if(currentBaseState.nameHash == idleState)
         {
             if(Input.GetKey(KeyCode.LeftShift))
             {
                 anim.SetBool ("Sprint", true);
             }
 
             else
             {
                 anim.SetBool("Sprint", 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

Answer by tanoshimi · Jan 08, 2014 at 03:58 PM

You need to test if(Input.GetKey(KeyCode.LeftShift)) while in runningState and set the Sprint book to true/false accordingly.

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 alexanderameye · Jan 08, 2014 at 04:09 PM 0
Share

I can't get it to work :( but what exactly do you mean?

avatar image alexanderameye · Jan 08, 2014 at 04:13 PM 0
Share

Ow ok yes now I understand! I got it working! Thank you!!

avatar image tanoshimi · Jan 08, 2014 at 04:14 PM 0
Share

You said "I have a runningstate for running". So, where in the script above do you handle keyboard input when you're in the running state? I see only idleState, locoState, rollState, and jumpState...

avatar image alexanderameye · Jan 08, 2014 at 04:21 PM 0
Share

I just created a new one, but the problem is solved thanks to you! :)

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

19 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

Related Questions

Don't play another animation if a specific animation is already playing? 0 Answers

While loop interfering with Mecanim? 0 Answers

What is the proper way to wait for an Animator Controller to update? 1 Answer

Mecanim Animations float til end of animation 1 Answer

Animation Gets Clipped When Speed Is Changed (Mecanim) 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