anoying animation problem (when "walking") 2d game

first of all. im making a 2d pixel game (with ragepixel). its very basic.

so, i’m heading right to the point.
my character moves the way i want it to, but the animations dont.

when holding down left or right arrow key it does the “WALK” animation.
and when im holding down both left and right the character should stop (witch it does) and then play the “STAND” animation.
the thing is when im walking, lets say right (holding down right arrow key), and then press the left arrow key it stops but continues the “WALK” animation. and then when im releasing the left arrow it moves right but plays the “STAND” animation.

I have no clue at all what to write in the script to “get rid of” the problem.

If you know the problem, could you explain whats wrong?
Thanks in advance!

Here is my Script:

using UnityEngine;
using System.Collections;

public class pmovetest : MonoBehaviour {
	
	IRagePixel ragepixel;
	
	private float walkSpeed = 10f;
	

	// Use this for initialization
	void Start () {
		
		ragepixel = GetComponent<RagePixelSprite>();
		
		ragepixel.SetSprite ("STAND");
		ragepixel.PlayNamedAnimation ("STAND");
	}
	
	// Update is called once per frame
	void Update () {	
		
		var inputLeft = Input.GetKey (KeyCode.LeftArrow);
		var inputLeftDown = Input.GetKeyDown (KeyCode.LeftArrow);
		var inputLeftUp = Input.GetKeyUp (KeyCode.LeftArrow);
		var inputRight = Input.GetKey (KeyCode.RightArrow);
		var inputRightDown = Input.GetKeyDown (KeyCode.RightArrow);
		var inputRightUp = Input.GetKeyUp (KeyCode.RightArrow);
		
		Vector3 moveDirection = new Vector3();
		
		// Gå vänster
		if (inputLeft  !inputRight)
		{
			moveDirection = new Vector3(-1f, 0f, 0f);
			if (inputLeftDown  !inputRightDown)
			{
				ragepixel.SetSprite("WALK");
				ragepixel.SetHorizontalFlip(true);
				ragepixel.PlayNamedAnimation("WALK");
			}
		}
		// Gå höger
		if (inputRight  !inputLeft)
		{
			moveDirection = new Vector3(1f, 0f, 0f);
			if (inputRightDown  !inputLeftDown)
			{
				ragepixel.SetSprite("WALK");
				ragepixel.SetHorizontalFlip(false);
				ragepixel.PlayNamedAnimation("WALK");
			}
		}
		
		
		if (inputLeftUp || inputRightUp)
		{
			ragepixel.SetSprite("STAND");
			ragepixel.PlayNamedAnimation("STAND");
		}
		
		
		transform.Translate (moveDirection * Time.deltaTime * walkSpeed);
	}
}

if you say:“and when im holding down both left and right the character should stop (witch it does) and then play the “STAND” animation.”

the you should make the statement:
if (inputRight inputLeft)

{
ragepixel.SetSprite(“STAND”);

ragepixel.PlayNamedAnimation(“STAND”);

}
the
if (inputLeftUp || inputRightUp)

{
should be used to play walking again in the oppesite direction of the one that had the Up.
if i read your problem right

thanks, that solves the problem. but of course there’s a new one… ^^

when simply walking left or right by holding down key left OR right and then release the key, the character continues the WALK animation, when it should go to STAND animation.
and when im holding down left AND right key it changes to the STAND sprite but wont animate.

im sorry for my stupidity but im still learning the basics!

could you post your current script that has this problem?

here you go! thanks for the superfast reply!

using UnityEngine;
using System.Collections;

public class pmovetest : MonoBehaviour {
	
	IRagePixel ragepixel;
	
	private float walkSpeed = 10f;
	

	// Use this for initialization
	void Start () {
		
		ragepixel = GetComponent<RagePixelSprite>();
		
		ragepixel.SetSprite ("STAND");
		ragepixel.PlayNamedAnimation ("STAND");
	}
	
	// Update is called once per frame
	void Update () {	
		
		var inputLeft = Input.GetKey (KeyCode.LeftArrow);
		var inputLeftDown = Input.GetKeyDown (KeyCode.LeftArrow);
		var inputLeftUp = Input.GetKeyUp (KeyCode.LeftArrow);
		var inputRight = Input.GetKey (KeyCode.RightArrow);
		var inputRightDown = Input.GetKeyDown (KeyCode.RightArrow);
		var inputRightUp = Input.GetKeyUp (KeyCode.RightArrow);
		
		Vector3 moveDirection = new Vector3();
		
		// Gå vänster
		if (inputLeft  !inputRight)
		{
			moveDirection = new Vector3(-1f, 0f, 0f);
			if (inputLeftDown  !inputRightDown)
			{
				ragepixel.SetSprite("WALK");
				ragepixel.SetHorizontalFlip(true);
				ragepixel.PlayNamedAnimation("WALK");			
			}

		}
		// Gå höger
		if (inputRight  !inputLeft)
		{
			moveDirection = new Vector3(1f, 0f, 0f);
			if (inputRightDown  !inputLeftDown)
			{
				ragepixel.SetSprite("WALK");
				ragepixel.SetHorizontalFlip(false);
				ragepixel.PlayNamedAnimation("WALK");
			}
			
		}
		
		if (inputRight  inputLeft)
		{
				ragepixel.SetSprite("STAND");
				ragepixel.PlayNamedAnimation("STAND");			
		}		

		if (inputLeftUp || inputRightUp)
		{
			ragepixel.SetSprite("WALK");
			ragepixel.PlayNamedAnimation("WALK");
			if (inputLeftUp  inputRight)
			{
				ragepixel.SetHorizontalFlip(false);
			}
			if (inputRightUp  inputLeft)
			{
				ragepixel.SetHorizontalFlip(true);
			}
		}
		
		if (Input.GetKeyDown(KeyCode.RightShift))
		{
			walkSpeed = 25f;
		}
		
		if (Input.GetKeyUp(KeyCode.RightShift))
		{
			walkSpeed = 10f;
		}

		transform.Translate (moveDirection * Time.deltaTime * walkSpeed);
	}
}

// Gå vänster = Walk Left

// Gå höger = Walk Right

hmm wouldn’t you be better of with something like this?

#pragma strict

var input:int;

function Update () 
{
	input=Input.GetAxis("Horizontal");
	if(input)
	{
		if(input == -1)
		{
			print("walk left");
		}
		else if(input == 1)
		{
			print("walk right");
		}
	}
	else
	{
		print("stand");
	}

}

Yeah, i just can’t get it to work. Thats why i have not used it. haha

just replace the print(…); with your ragepixel.whatever()

ah and i see you are using C#
replace var input:int; with private int input;
and function Update () with void Update ()
the rest stays the same on first looks.

yeah, i tried that before, but it just dont work.
i got an error who says: No overload for method GetAxis' takes 2’ arguments.

then what are you writing in between the () after GetAxis?

just “Horizontal”.
should it be more than just that?

hi! again… :stuck_out_tongue:

ok, so now i’ve wrote a script with GetAxis instead of GetKey.
the thing is that he walks right even if i hold down left arrow.

this started to happen after adding amtToMove to: transform.Translate(Vector3.left * amtToMove);

so, it has something to do with the amtToMove Code i guess. but i cant figure out what.

using UnityEngine;
using System.Collections;

public class lastMoveTest : MonoBehaviour {
	
	IRagePixel ragepixel;
	
	public float playerSpeed;
	private float direction;
	
	// Use this for initialization
	void Start () {
		
		ragepixel = GetComponent <RagePixelSprite>();
		
		ragepixel.SetSprite("STAND");
		ragepixel.PlayNamedAnimation("STAND");
		
		playerSpeed = 13;
	}
	
	// Update is called once per frame
	void Update () {
		
		float amtToMove;
		
		direction = Input.GetAxis("Horizontal");
		amtToMove = direction * Time.deltaTime * playerSpeed;
		
		if (direction < 0)
		{
			transform.Translate(Vector3.left * amtToMove);
		}
		else if (direction > 0)
		{
			transform.Translate(Vector3.right * amtToMove);
		}
		else if (direction == 0)
		{

		}
		
		
	
	}
}

i solved it!
had to delete direction from amtToMove.