首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >玩家没有移动到所有的方向点。

玩家没有移动到所有的方向点。
EN

Stack Overflow用户
提问于 2018-11-23 05:22:06
回答 1查看 102关注 0票数 0

敌人的角色不能移动到第三个方向点。移动到路点2后,它就停止了,空闲的动画播放。角色有一个NavMeshAgent,当他到达路径点时,目标到达的事件似乎不会被触发。如果在此之前有人遇到过这样的情况,我会很感激你提供任何可能的信息。几个小时以来,我一直在试图弄清楚到底出了什么问题,现在我开始认为这可能不是任何一个脚本。

这是路点控制器

代码语言:javascript
复制
using UnityEngine;
using UnityEngine.AI; 

public class WaypointController : MonoBehaviour {

    Waypoints[] waypoints;
    public Transform target; 
    //NavMeshPath path; 
    int currentWaypointIndex = -1;
    //private NavMeshAgent agent;
    //EnemyCharacter enemy; 

    public event System.Action<Waypoints> OnWaypointChanged;

    // Use this for initialization
    void Awake () {

        waypoints = GetWaypoints();

    }

    public void SetNextWaypoint() {

        if (currentWaypointIndex != waypoints.Length)
            currentWaypointIndex++;

        if (currentWaypointIndex == waypoints.Length)
            currentWaypointIndex = 0;

        if (OnWaypointChanged != null)
            OnWaypointChanged(waypoints[currentWaypointIndex]);
        //Debug.Log("OnWaypointChanged == null: " + (OnWaypointChanged == null));
        //Debug.Log("OnWaypointChanged != null: " + (OnWaypointChanged != null));

    }
    Waypoints[] GetWaypoints()
    {
        return GetComponentsInChildren<Waypoints>();

    }
    private void OnDrawGizmos()
    {
        Gizmos.color = Color.red;

        Vector3 previousWaypoint = Vector3.zero;
        foreach (var waypoint in GetWaypoints())
        { 
            Vector3 waypointPosition = waypoint.transform.position; 
            Gizmos.DrawWireSphere(waypointPosition, .2f);
            if (previousWaypoint != Vector3.zero)
                Gizmos.DrawLine(previousWaypoint, waypointPosition);
            previousWaypoint = waypointPosition;
        }
    }
}

下面是EnemyPatrolPoints脚本

代码语言:javascript
复制
using UnityEngine;

[RequireComponent(typeof(AI_PathFinder))]
public class EnemyPatrolPoints : MonoBehaviour {

    [SerializeField]
    WaypointController waypointController;

    [SerializeField]
    float waitTimeMin;

    [SerializeField]
    float waitTimeMax;

    AI_PathFinder pathfinder;

    private void Start()
    {
        waypointController.SetNextWaypoint(); 
    }

    private void Awake()
    {
        pathfinder = GetComponent<AI_PathFinder>();
        pathfinder.OnDestinationReached += Pathfinder_OnDestinationReached;
        waypointController.OnWaypointChanged += WaypointController_OnWaypointChanged;
    }
    private void WaypointController_OnWaypointChanged(Waypoints waypoint)
    {
        pathfinder.SetTarget(waypoint.transform.position);
        print("waypoint changed"); 
    }
    private void Pathfinder_OnDestinationReached()
    {
        SealForce_GameManager.Instance.Timer.Add(waypointController.SetNextWaypoint, Random.Range(waitTimeMin, waitTimeMax));
        print("destination reached"); 
    }   
}

这是AI开拓者脚本

代码语言:javascript
复制
using UnityEngine;
using UnityEngine.AI;

[RequireComponent(typeof(NavMeshAgent))]
public class AI_PathFinder : MonoBehaviour
{

    [HideInInspector]
    public NavMeshAgent agent;
    public EnemyPatrolPoints enemyPatrolPoints;

    [SerializeField] float distanceRemainingThreshold;

    bool m_destinationReached;
    bool destinationReached
    {
        get
        { return m_destinationReached; }

        set
        {
            m_destinationReached = value;

            if (m_destinationReached)
            {
                if (OnDestinationReached != null)
                    OnDestinationReached();
            }
        }
    }

    public event System.Action OnDestinationReached;

    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
        //enemyPatrolPoints = GetComponent<EnemyPatrolPoints>();
    }
    public void SetTarget(Vector3 target)
    {
        agent.SetDestination(target);
    }

    void Update()
    {
        if (destinationReached)
            return;

        if (agent.remainingDistance < distanceRemainingThreshold)
            destinationReached = true;


    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-23 06:17:05

线

代码语言:javascript
复制
if (agent.remainingDistance < distanceRemainingThreshold)
    destinationReached = true;

,只要destinationReachedtrue,就永远不会到达

代码语言:javascript
复制
if (destinationReached)
    return;

在到达第一个路径点之后,将其设置为true,然后永远不要将其重置为false,因此您的Update在将来总是被跳过。

你应该把它添加到

代码语言:javascript
复制
public void SetTarget(Vector3 target)
{
    agent.SetDestination(target);
    destinationReached = false;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53441012

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档