Need help with sprite animating

OK, so I’m new to unity and me and a friend want to make a 2D game. So I’ve followed a tutorial on youtube and write this script:

Now, I’ve done some simple coding and programming before (but never in javascript) and i can understand the code.

With this script the animation changes between walking / jumping / idle (it is connected to this javascript http://wiki.unity3d.com/index.php/Animating_Tiled_texture_-_Extended), but when space (the jump button) is pushed and the sprite jumps, if you keep holding space the character will still be in the jump animation. How c

So here comes the question:

What can I do to make the character change back to the idle animation when hitting the ground (when the player is holding the jump button)?

And can I have another animation for when the character is on his way down?

Note: English is not my first language so I’m sorry that my formulation isn’t the best

#pragma strict




function Start () {

}

function Update () {
	
	var AT = gameObject.GetComponent(AnimateTexture); //Store the AnimateTexture script

	if(Input.GetKey("space")) { //Player jumps
		AT.rowNumber = 2; //Start jumping animation
	}

	else if(Input.GetKey("a")) { //Player moves left
		AT.rowNumber = 1; //Start running animation
	}
		
	else if(Input.GetKey("d")) { //Player moves right
		AT.rowNumber = 1; //Start running animation
	}
	
	else if(Input.GetKey("left")) { //Player moves left
		AT.rowNumber = 1; //Start running animation
	}
	
	else if(Input.GetKey("right")) { //Player moves right
		AT.rowNumber = 1; //Start running animation
	}
	

	
	else { //Player doesn't press anything
		AT.rowNumber = 0; //Return back to the idle animation
	}
	
	
}

1 Answer

1

At the moment animation is dependent entirely on input…

Normally animation would depend on character state (walking, jumping, punching etc).

Then you would allow the environment and player input to control character state.

For example:

  • if the character is grounded (known to be on the ground) and the player presses jump then let the character jump (apply vertical movement and play the jump animation).
  • if the character is grounded and the player enters no input play the idle animation.

With these two rules the player will transition idle to and back to idle appropriately…

As you can see the correct animation depends the character state, which is effected by both input and environment.