【问题标题】:Unity3D: Returning back to idle state animationUnity3D:返回空闲状态动画
【发布时间】:2016-07-16 14:40:17
【问题描述】:

我在游戏中返回空闲状态动画时遇到问题。这个video 是我的问题的一个例子。我的播放器不会回到空闲状态动画,我确实尝试将其添加到我的代码中:

anim.SetBool ("walking", false); 

(这放在我的code 的末尾附近)

但是这个happens。这不是我想要的。在我向您展示的first 视频中,它显示我单击了walking parameter off,这样我就可以向您展示如果我的播放器在到达目的地后停止时我的动画会是什么样子。在视频中您可以看到我的播放器面对走下来走错了路,它面对这个way和那个way,我也想修复,但不知道如何修复。那么任何人都可以帮助我解决我的两个问题,即:

  1. 一旦我的播放器到达目的地,就会返回空闲动画。
  2. 并确保我的播放器朝向正确的方向。我的玩家精灵image

这是我的代码:

二次编辑

private Animator anim;
public float speed = 15f;
private Vector3 target;
private bool touched;
private bool playerMovementRef;

void Start () 
{
    target = transform.position;
    anim = GetComponent<Animator> ();
}

void Update () 
{

if(transform.position == target)
{
    anim.SetBool ("walking", false);
}
    if (Input.GetMouseButtonDown (0))
       {
        Vector3 mousePosition = Input.mousePosition;
        mousePosition.z = 10; // distance from the camera
        target = Camera.main.ScreenToWorldPoint (mousePosition);
        target.z = transform.position.z;

        var movementDirection = (target - transform.position).normalized;
            Vector3 animDirection = Vector3.zero;
            // Use >= to default to horizontal on both being equal

        if (movementDirection.x > movementDirection.y) 
            animDirection.x = 1;
        else
            animDirection.y = 1;

        anim.SetBool ("walking", true);
        anim.SetFloat ("SpeedX", movementDirection.x);
        anim.SetFloat ("SpeedY", movementDirection.y);

        Debug.LogFormat ("X: {0}, Y: {1}", movementDirection.x, movementDirection.y);

        if (movementDirection.x > 0)
        {
            anim.SetFloat ("LastMoveX", 1f);
        }
        else if (movementDirection.x < 0) 
        {
            anim.SetFloat ("LastMoveX", -1f);
        } 
        else 
        {
            if (movementDirection.y > 0)
            {
                anim.SetFloat ("LastMoveY", 1f);
            } 
            else if (movementDirection.y < 0)
            {
                anim.SetFloat ("LastMoveY", -1f);
            } 
            else 
            {
                anim.SetFloat ("LastMoveY", 0f);
            } 
        }   
    } 
    else 
    {
            transform.position = Vector3.MoveTowards (transform.position, target, speed * Time.deltaTime);
    }
}

【问题讨论】:

    标签: c# animation unity3d


    【解决方案1】:

    检测玩家是否在当前目的地。您可以从target 获取当前目的地。如果它在当前目的地,则将“walking”布尔值设为 false。面对正确的方向应该可以正常工作。

    将此块放入Update():

    if(transform.position == target)
    {
        anim.SetBool ("walking", false);
    }
    

    【讨论】:

    • 感谢您的回复。我已经按照您的建议进行了尝试,但我仍然拥有problems。也许是因为我把你给我的块放错了地方,如果你回头看我上面的问题,你会发现我已经把代码编辑成了现在的样子。
    猜你喜欢
    • 1970-01-01
    • 2020-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-16
    • 1970-01-01
    • 1970-01-01
    • 2020-11-14
    相关资源
    最近更新 更多