- Home /
How do I get my jump animation to work?
I finally got jumping in my game, but now I want him to switch to his jump animation when he is not grounded. I tried putting it with the void jump and within the void OnCollisionEnter, as well as under the jump button in Update, but it does not play.
using UnityEngine;
using System.Collections;
using SpriteFactory;
public class player : MonoBehaviour {
public float speed = 1.0f;
public float jumpSpeed = 10.0f;
public float gravity = 20.0f;
public bool grounded = true;
private Transform thisTransform;
private Sprite sprite;
private bool flipped;
// Use this for initialization
void Awake () {
thisTransform = transform;
sprite = (Sprite)GetComponent(typeof(Sprite));
}
// Update is called once per frame
void Update () {
Vector3 moveVector = new Vector3(0,0,0);
bool moving = false;
moveVector.y -= gravity * Time.deltaTime;
if(Input.GetKeyDown(KeyCode.Space)){
Jump();
}
if(Input.GetKey(KeyCode.A)){
moveVector.x -= 1.0f;
moving = true;
}
if(Input.GetKey(KeyCode.D)){
moveVector.x += 1.0f;
moving = true;
}
if(Input.GetKey(KeyCode.Mouse0)){
Attack();
} else {
if(!IsAttacking()) {
if(moving) {
Move(moveVector);
} else {
Stand();
}
}
if(Input.GetKey(KeyCode.S)){
Crouch();
} else {
if(!IsCrouching()) {
}
if(moving) {
Move(moveVector);
}
else {
Stand();
}
}
}
}
private void Stand() {
sprite.Stop();
}
private void Move(Vector3 moveVector) {
FlipSprite(moveVector.x);
moveVector *= speed * Time.deltaTime;
thisTransform.position = thisTransform.position + moveVector;
sprite.Play("walk");
}
private void FlipSprite(float x) {
if(x == 0.0f) return;
float xDir = Mathf.Sign(x);
if(!flipped && xDir < 0.0f) {
flipped = true;
sprite.SetFlippedState(true, false);
} else if(flipped&& xDir > 0.0f) {
flipped = false;
sprite.SetFlippedState(false, false);
}
}
private void Attack() {
if(IsAttacking()) return;
sprite.Play("Sword");
}
private bool IsAttacking() {
if(sprite.IsAnimationPlaying("Sword")){ return false;}
return false;
}
private void Crouch() {
if(IsCrouching()) return;
sprite.Play("crouch");
}
private bool IsCrouching() {
if(sprite.IsAnimationPlaying("crouch")) {return false;}
return false;
}
void Jump(){
if(grounded == true)
{
rigidbody.AddForce(Vector3.up * jumpSpeed);
grounded = false;
}
}
void OnCollisionEnter(Collision col){
if(col.gameObject.tag == "floor"){
grounded = true;
sprite.Play("jump");
}
}
}
Add
Debug.Log("Now standing.");
to your Stand() function and see if that immediately occurs after your jump. I think your placement of the sprite.Play("jump") in the OnCollision might be getting immediately overwritten by the code falling through to Stand() and sprite.Stop(). Is there a reason why you don't have sprite.Play("jump") in Jump()?
Like in my comment I have tried placing it in the void called jump. Here is what I have tried:
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)){
Jump();
sprite.Play("jump");
void Jump(){
if(grounded == true)
{
rigidbody.AddForce(Vector3.up * jumpSpeed);
grounded = false;
sprite.Play("jump");
}
}
Also Tried this:
void Jump(){
if(grounded == true)
{
rigidbody.AddForce(Vector3.up * jumpSpeed);
grounded = false;
}
if(grounded == false){
sprite.Play("jump");
}
}
There is a problem with how you check if you are grounded. Using OnCollisionEnter means that any collision will make the guy grounded. Suppose you jump and hit with the head, you are grounded.
If you want to make sure you are hitting with the feet, you can use the capsule and check which part has hit or a raycast downward.
I see that you are checking if it is ground but I bet a platform placed above that you are supposed to access is also tagged as ground.
Answer by fafase · Sep 10, 2013 at 04:12 PM
Add a raycast that goes from the center of the guy towards the ground at a distance of half the guy's height. Then if the raycast is false it means you are in the air, play jump animation.
Transform tr;
float height;
int layerMask;
void Start(){
tr = transform;
height = collider.bounds.extent.y;
layerMask = LayerMask.NameToLayer("Player");
}
void Update(){
if(!Physics.Raycast(tr.position, Vector3.down, height,layerMask)){
anim.CrossFade("Jump");
}
}
Best would also be to check if you are jumping or falling since you may not play the same animation for each. The layer allows the cast to go through the player or ignore the player.
This stops the walking animation in the air, but doesn't play animation. I have use the sprite.Play from sprite factory for an animation on my character to play. I should have said this at the beginning. I'm making a 2D side-scroller with Sprite Factory which I have to put Sprite.(Function/Call/whateverelse) to get animations to play.
I'm fine with it stopping the walking animation in the air, but now with what I added caused my attack part of the script to continuously hurt the enemy ins$$anonymous$$d of having to hit the button every time to deal damage.
Fact is I do not know how you use this sprite thing. But since the walk anim stops it is just about triggering the jump anim when in the air. And yes s is missing.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
how do I script this jump animation? 1 Answer
jumping and landing workflow 0 Answers
Help with adding jump function to player. 2 Answers
How To Make Enemy Chase You? 1 Answer