当玩家进入特定距离时开始动画

Start animation when player enters specific distance

我想在我的播放器进入到某个对象的特定距离时启动动画,但我的动画没有启动。关于为什么它没有启动的任何建议?

这是我目前的代码:

using UnityEngine;
using System.Collections;
public class MoveTo : MonoBehaviour {
    public Transform Player;
    public Transform goal;
    public Animator ani;
    public Animator ani2;
    // Use this for initialization
    void Start ()
    {
        ani.Stop (); // this stop function is working accurate
        ani2.Stop ();
    }
    void Update()
    {
        float dist = Vector3.Distance (Player.position, transform.position);
        if (dist < 5)
        {
            ani.Play("Horse_Walk");// this is not working (Horse_Walk) is a state name
            ani2.Play("Horse_Run");
            pstart();
        }
    }
    void pstart(){
        NavMeshAgent agent=GetComponent<NavMeshAgent>();
        agent.destination = goal.position;
    }
}

可能有多种原因,包括(1)字符串错误(2)字符串名称不匹配,最后似乎更合适的原因是您再次播放动画。你应该需要用 bool

来调用它
    bool isPlayAnim =true;
        void Update()
            {
                float dist = Vector3.Distance (Player.position, transform.position);
                if (dist < 5 && isPlayAnim)
                {
                    isPlayAnim = false;//again true it on you specfic event
                    ani.Play("Horse_Walk");// this is not working (Horse_Walk) is a state name
                    ani2.Play("Horse_Run");
                    pstart();
                }
            }

isPlayAnim bool 你可以制作并使用它来播放动画一次,并在特定事件中再次实现它。

或者,您需要使用对撞机及其事件来 运行 此更新代码。我想这是完成这项工作的最佳方式。