- Home /
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;
}
}
}
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 :)
Sorry, i added it now. :) Feel free to correct my stupidness ^^
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.
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")));
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);
}
}