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

The question is answered, right answer was accepted

avatar image
0
Question by RicardoAntezana · Mar 01, 2015 at 09:26 AM · animationgetkeydownanimation.playgetkeyup

Walking and Running Script not working.

Hey, I tried to make a script where if I click/hold "w" the character would walk and if I click/hold "w" and "leftShift" at the same time he would sprint. Any help is appreciated. He walks fine but when I try to sprint he just preforms the walk animation. Here is my script:

 #pragma strict
 
 var ArmsWalkingForward : AnimationClip;
 var SprintAnimation    : AnimationClip;
 
 var IsWalking          : boolean = false;
 var IsSprinting        : boolean = false;
 
 function Start ()
 {
 
 }
 
 function Update ()
 {
     if(Input.GetKeyDown(KeyCode.W) && !IsSprinting)
     {
         IsWalking = true;
     }
     
     if(Input.GetKeyUp(KeyCode.W) && !IsSprinting)
     {
         IsWalking = false;
     }
     
     if(Input.GetKeyDown(KeyCode.W) && Input.GetKeyDown(KeyCode.LeftShift) && !IsWalking)
     {
         IsSprinting = true;
     }
     
     if(Input.GetKeyUp(KeyCode.W) && Input.GetKeyDown(KeyCode.LeftShift) && !IsWalking)
     {
         IsSprinting = false;
     }
     
     //Actions
     
     if(IsSprinting == true)
     {
         Sprint();
     }
     
     if(IsWalking == true)
     {
         Walk();
     }
 }
 
 function Sprint()
 {
     animation.Play("SprintAnimation");
 }
 
 function Walk()
 {
     animation.Play("ArmsWalkingForward");
 }
Comment
Add comment · Show 1
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 RicardoAntezana · Mar 01, 2015 at 09:19 AM 0
Share

He walks fine but when i try sprint he just performs the walk animation.

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Mmmpies · Mar 01, 2015 at 09:26 AM

You just the logic wrong, try this:

 #pragma strict
  
  var ArmsWalkingForward : AnimationClip;
  var SprintAnimation    : AnimationClip;
  
  var IsWalking          : boolean = false;
  var IsSprinting        : boolean = false;
  
  function Start ()
  {
  
  }
  
  function Update ()
  {
      if(Input.GetKeyDown(KeyCode.W))
      {
          IsWalking = true;
      }
      
      if(Input.GetKeyUp(KeyCode.W))
      {
          IsWalking = false;
      }
      
      if(Input.GetKeyDown(KeyCode.LeftShift))
      {
          IsSprinting = true;
      }
      
      if(Input.GetKeyUp(KeyCode.LeftShift))
      {
          IsSprinting = false;
      }
      
      //Actions
      
      if(IsSprinting == true && IsWalking == true)
      {
          Sprint();
      }
      
      if(IsWalking == true && !IsSprinting)
      {
          Walk();
      }
  }
  
  function Sprint()
  {
      animation.Play("SprintAnimation");
  }
  
  function Walk()
  {
      animation.Play("ArmsWalkingForward");
  }
Comment
Add comment · Show 3 · 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 RicardoAntezana · Mar 01, 2015 at 09:59 AM 0
Share

Thank you almost working perfectly the only problem is he doesn't stop walking.

avatar image Mmmpies · Mar 01, 2015 at 10:04 AM 0
Share

O$$anonymous$$ I didn't have your animations so when I tested it I just Debug.Log to show walking or sprinting. It stops calling the Sprint or Walk but you've got no function to stop the animation so something like this:

 // In update
 if(!IsWalking)
 {
     Idle();
 }
 
 
 // then
 function Idle()
 {
     animation.Play("Idle");
 }

Assu$$anonymous$$g you have an idle animation called Idle.

avatar image RicardoAntezana · Mar 01, 2015 at 03:23 PM 0
Share

Haha that does it it is now working thanks!

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Play half of Animation on KeyDown/Other Half on KeyUp 1 Answer

Can/how do I set up a boolean to continue an animation after button press 1 Answer

Animation Playing Automatically Even When No Input Is Given 1 Answer

Character Animation Assistance 1 Answer

Another "Animation.Play() not working" question! 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