无法在空降时播放完整的跳跃动画

时间:2015-12-31 13:44:43

标签: c# animation unity3d

我试图控制一组角色动画,但我遇到了跳跃动画的问题。我的水平步行动画工作正常但是当我按下跳跃按钮时,我希望根据玩家是处于闲置还是行走状态来播放两个跳跃动画中的一个。

我根据我的角色对撞机是否正在接地而设置了一个更新的bool,我检查确定何时播放跳跃动画。然而,当我按下跳跃按钮时,它只播放相应跳跃动画的第一帧,然后再回到空闲或步行。当按下跳跃按钮并且角色在空中时,如何让整个跳跃动画播放?

using UnityEngine;
using System.Collections;

public class AsherBlackMover : MonoBehaviour {

    public float moveSpeed = 3;
    public float rotateSpeed = 10;
    public Transform graphics;
    public SkeletonAnimation skeletonAnimation;
    public Vector2 jumpVector;
    public bool isGrounded;

    // isGrounded Variables
    public Transform grounderPosition;
    public float grounderRadius;
    public LayerMask grounderLayerMask;

    private Rigidbody2D asherBlackRB;
    private Animator asherBlackAnim;

    Quaternion goalRotation = Quaternion.identity;
    float xDir;
    string currentAnimation = "";

    void Start ()
    {
        // Create a reference to Asher Black Rigidbody2D
        asherBlackRB = GetComponent<Rigidbody2D>();
    }

    void Update ()
    {
        xDir = Input.GetAxis ("Horizontal") * moveSpeed;

        if (xDir > 0 && isGrounded == true) // ------ Walk Right
        {
            goalRotation = Quaternion.Euler (0, 0, 0);
            SetAnimation ("Walk", true);
        } 
        else if (xDir < 0 && isGrounded == true) // ------ Walk Left
        {
            goalRotation = Quaternion.Euler (0, 180, 0);
            SetAnimation ("Walk", true);
        }
        else if (isGrounded == true) // ------ Idle
        {
            SetAnimation ("Idle", true);
        }

        // Jump Button
        if (Input.GetKeyDown("space") && isGrounded == true)
        {
            isGrounded = false;
            skeletonAnimation.state.SetAnimation (0, "Idle-Jump", true);

            if (xDir > 0 && isGrounded == false)
            {
                Jump ("Walk-Jump");
            }
            else if (xDir < 0 && isGrounded == false)
            {
                Jump ("Walk-Jump");
            }
            else if (isGrounded == false)
            {
                Jump ("Idle-Jump");
            }
        }

        // Circle on character that determines when grounded or not
        isGrounded = Physics2D.OverlapCircle (grounderPosition.transform.position, grounderRadius, grounderLayerMask);

        // Flip character smothly to emulate paper mario effect
        graphics.localRotation = Quaternion.Lerp (graphics.localRotation, goalRotation, rotateSpeed * Time.deltaTime);
    }

    void OnDrawGizmos ()
    {
        Gizmos.color = Color.yellow;
        Gizmos.DrawWireSphere (grounderPosition.transform.position, grounderRadius);
    }

    void SetAnimation (string name, bool loop)
    {
        if (name == currentAnimation)
        {
            return;
        }
        skeletonAnimation.state.SetAnimation (0, name, loop);
        currentAnimation = name;
    }

    void Jump (string animName)
    {
        asherBlackRB.AddForce (jumpVector, ForceMode2D.Force);
        SetAnimation (animName, true);

        print ("Doing a jump using the " + animName + " animation");
    }

    // Physics Updates
    void FixedUpdate ()
    {
        asherBlackRB.velocity = new Vector2 (xDir, asherBlackRB.velocity.y);
    }
}

1 个答案:

答案 0 :(得分:0)

据我所知,在// Jump Button if块中您将isGrounded设置为false,但在此之后您根据碰撞检测设置isGrounded

isGrounded = Physics2D.OverlapCircle (grounderPosition.transform.position, grounderRadius, grounderLayerMask);

当你到达这个语句时,你仍在处理相同的帧,并且当你到达更新函数结束时,角色仍然没有时间移动isGrounded。在下一次更新时,动画将通过第一组if / else块恢复为walk / idle。您可能希望将冲突检测语句放在跳转if的else块中。这可能还不够,因为帧之间的时间可能不足以让角色远离地面移动得足够远。在这种情况下,你可能会拿一个计数器并计算5帧,直到你回去检查与地面的碰撞。