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

时间:2016-05-27 15:32:20

标签: c# unity3d unity5

我想在我的播放器输入与对象的特定距离时启动动画,但我的动画不能启动。关于它为什么不开始的任何建议?

这是我到目前为止的代码:

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 个答案:

答案 0 :(得分:1)

可能有几个原因包括(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你可以制作和使用只播放动画一次,并在特定事件中再次使用它。

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