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 Dee91 · Dec 27, 2013 at 12:24 AM · animation2d-platformerrunwalk

idle > walk > run animation

Hey i'm making a 2d platformer. I have managed to get my 2d sprite to idle and walk. Now i'm trying to add a run fuction. So, when you press Shift you go faster. This works, how ever the run animation doesn't play?

I have the animator set up like this:

idle (speed > 0.1) walk (speed > 4.0) run

And similar to going back to walking and idle ofcourse.

Am i doing this wrong?

code:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
     public float speed = 4.0f;
     private Transform myTransform;
 
     Animator anim;
 
     // Use this for initialization
     void Start () {
         myTransform = transform;
         anim = GetComponent<Animator> ();
     }
     
     // Update is called once per frame
     void Update () {
         //animation
         anim.SetFloat("speed", Mathf.Abs(Input.GetAxis("Horizontal")));
         //move horizontal
         myTransform.Translate(Vector2.right * Input.GetAxis ("Horizontal") * speed * Time.deltaTime);
 
         if(Input.GetKey (KeyCode.LeftShift)) {
             speed = 8.0f;
 
         } else if(!Input.GetKey (KeyCode.LeftShift)){
             speed = 4.0f;
         }
     }
 }
 
Comment
Add comment · Show 3
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 knuckles209cp · Dec 27, 2013 at 12:38 AM 0
Share

if you can give us your code , we can help you :) , so yea just clikc the 101010 button above the rply area and paste your code and we can help you out :)

avatar image Dee91 · Dec 27, 2013 at 12:47 AM 0
Share

Sorry, i added it now. :) Feel free to correct my stupidness ^^

avatar image Bmarlyman21 · Dec 27, 2013 at 05:05 PM 0
Share

If ( Input.Get$$anonymous$$eyUp(whatever key you want) ) { animation.Play("Idle") } else if ( Input.Get$$anonymous$$eyDown(Whatever key) ) { animation.Play("Walk") } else if ( Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftShift) && Input.Get$$anonymous$$eyDown(Whatever key) ) { animation.Play("Run") }

that covers the animation part.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by HappyMoo · Jan 01, 2014 at 03:13 AM

You only insert floats from 0 to 1 into the speed parameter....

 anim.SetFloat("speed", Mathf.Abs(Input.GetAxis("Horizontal")));

should be something like:

 anim.SetFloat("speed", speed*Mathf.Abs(Input.GetAxis("Horizontal")));
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 HappyMoo · Jan 01, 2014 at 03:21 AM 0
Share

Also notice that asking your Input functions multiple time is bad style. I would change it to:

 public class PlayerController : $$anonymous$$onoBehaviour
 {
     public float walkSpeed = 4.0f;
     public float runSpeed = 8.0f;
     
     private float current$$anonymous$$axSpeed = 0;
     private Animator anim;
  
     void Start () {
        anim = GetComponent<Animator> ();
     }
  
     void Update () {
 
        if(Input.Get$$anonymous$$ey ($$anonymous$$eyCode.LeftShift)) {
          current$$anonymous$$axSpeed = runSpeed;
        } else {
          current$$anonymous$$axSpeed = walkSpeed;
        }
 
        float speed = current$$anonymous$$axSpeed * Input.GetAxis("Horizontal");
        anim.SetFloat("speed", $$anonymous$$athf.Abs(speed));
        transform.Translate(Vector2.right * speed * Time.deltaTime); 
     }
 }

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

Lerpz ThirdPersonPlayerAnimation bug - running at walking speed 1 Answer

How do i make my player go from walk animation to run animation using blend trees? 2 Answers

A node in a childnode? 1 Answer

Speed of animation transitoins 2 Answers

walk to run animation 2 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