玩家行走时如何修复2D动画?

时间:2019-10-18 11:20:45

标签: c# unity3d

我正在尝试使角色闲置,奔跑,跳跃和摔倒。我做到了,但是我在运行动画时遇到了错误。当他向左或向右走时,他不播放动画“ run”,该动画由参数“ 2”的整数“ noob”调用,他播放动画“ fall”,其中“ noob”是参数“ 4”

我正在使用Unity 4.5.x和C#代码

using UnityEngine;
using System.Collections;

public class movement : MonoBehaviour {

Rigidbody2D rb;
Animator anim;
public int jumpCount = 0;
public Camera main;
float jumpTimer = 0;

void Start () {
    rb = GetComponent<Rigidbody2D> ();
    anim = GetComponent<Animator>();
}

void Update () {
    if (Input.GetKeyDown (KeyCode.X) && jumpCount < 2 && jumpTimer == 0) {
        jump ();
        jumpCount += 1;
        jumpTimer = 1;
        StartCoroutine("jumpTime");
    }
    if (rb.velocity.y > 0) {
        Flip();
        anim.SetInteger ("noob", 3);
    }
    if (rb.velocity.y < 0) {
        Flip();
        anim.SetInteger ("noob", 4);
    }
    if (Input.GetAxis ("Horizontal") == 0 && rb.velocity.y == 0) {
        anim.SetInteger ("noob", 1);
    }
    if (rb.velocity.y == 0 && Input.GetAxis ("Horizontal") != 0) {
        Flip();
        anim.SetInteger("noob", 2);
    }
    if (rb.velocity.y == 0) {
        jumpCount = 0;
    }
}

void FixedUpdate(){
    rb.velocity = new Vector2 (Input.GetAxis ("Horizontal") * 12f, rb.velocity.y);
}

void jump(){
    rb.AddForce (transform.up * 14F, ForceMode2D.Impulse);
}
void Flip() {
    if (Input.GetAxis ("Horizontal") > 0) {
        transform.localRotation = Quaternion.Euler (0, 0, 0);
    }
    if (Input.GetAxis ("Horizontal") < 0) {
        transform.localRotation = Quaternion.Euler (0, 180, 0);
    }
}
IEnumerator jumpTime() {
    yield return new WaitForSeconds (0.4f);
    jumpTimer = 0;
}
}

我尝试使用jumCount,jumpTimer和velocity.y做一些事情,但没有帮助

1 个答案:

答案 0 :(得分:1)

我建议您制作一个接地的跳跃式布尔,您可以使用该布尔来确定您的角色是否是空运。如果条件语句也可以使用else来帮助避免多次设置您的值。然后,您可以执行以下操作:

if (grounded && jumping) {
    Flip();
    anim.SetInteger ("noob", 3);
    jumping = false;
}
else if (!grounded) {
    Flip();
    anim.SetInteger ("noob", 4);
}
else if (Input.GetAxis ("Horizontal") == 0 && grounded) {
    anim.SetInteger ("noob", 1);
}
else if (grounded && Input.GetAxis ("Horizontal") != 0) {
    Flip();
    anim.SetInteger("noob", 2);
}