首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >敌人一旦到达某一点就停止了,有没有办法调整它,使它停止这样做呢?

敌人一旦到达某一点就停止了,有没有办法调整它,使它停止这样做呢?
EN

Stack Overflow用户
提问于 2022-11-28 02:32:08
回答 1查看 38关注 0票数 -3

我最近换了我的运动剧本。然而,我的敌人现在只是走到一个特定的点,然后完全停止移动。

  • ,我想知道我是否可以在代码中添加或更改任何东西,使其在不追逐玩家的同时连续移动??

以下是代码:

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

public class StalkerAI : MonoBehaviour
{
    public NavMeshAgent agent;
    public Transform Player;

    public float range; //radius of sphere
    public Transform centrePoint; //centre of the area the agent wants to move around in
    //instead of centrePoint you can set it as the transform of the agent if you don't care about a specific area


    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
    }
    // Update is called once per frame
    void Update()
    {
        float distVal = 5.0f;
        float dist = Vector3.Distance(agent.transform.position, Player.position);

        if (dist <= distVal)
        {
            agent.SetDestination(Player.position);
        }
        else
        {
            if (agent.remainingDistance <= agent.stoppingDistance) //done with path
            {
                Vector3 point;
                if (RandomPoint(centrePoint.position, range, out point)) //pass in our centre point and radius of area
                {
                    Debug.DrawRay(point, Vector3.up, Color.blue, 1.0f); //so you can see with gizmos
                    agent.SetDestination(point);
                }
            }

        }
    }

    bool RandomPoint(Vector3 center, float range, out Vector3 result)
    {

        Vector3 randomPoint = center + Random.insideUnitSphere * range; //random point in a sphere 
        NavMeshHit hit;
        if (NavMesh.SamplePosition(randomPoint, out hit, 1.0f, NavMesh.AllAreas)) //documentation: https://docs.unity3d.com/ScriptReference/AI.NavMesh.SamplePosition.html
        {
            //the 1.0f is the max distance from the random point to a point on the navmesh, might want to increase if range is big
            //or add a for loop like in the documentation
            result = hit.position;
            return true;
        }

        result = Vector3.zero;
        return false;
    }

}
EN

回答 1

Stack Overflow用户

发布于 2022-11-28 05:02:48

我想这就是你要找的行为。您的问题是,而且可能是因为您将空vector3传递给您的RandomPoint方法,所以您被否决了投票。

如果您花了几分钟时间来阅读代码中的注释,那么您将确切地看到如何使用它。

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

public class StalkerAI : MonoBehaviour
{
    public NavMeshAgent agent;
    public Transform Player;

    public float range; //radius of sphere
    public Vector3 centrePoint; //centre of the area the agent wants to move around in
    //instead of centrePoint you can set it as the transform of the agent if you don't care about a specific area


    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
    }
    // Update is called once per frame
    void Update()
    {
        float distVal = 5.0f;
        float dist = Vector3.Distance(agent.transform.position, Player.position);

        if (dist <= distVal)
        {
            agent.SetDestination(Player.position);
        }
        else
        {
            if (agent.remainingDistance <= agent.stoppingDistance) //done with path
            {
                centrePoint = transform.position;

                if (RandomPoint(centrePoint, range, out centrePoint)) //pass in our centre point and radius of area
                {
                    Debug.DrawRay(centrePoint, Vector3.up, Color.blue, 1.0f); //so you can see with gizmos
                    agent.SetDestination(centrePoint);
                }
            }

        }
    }

    bool RandomPoint(Vector3 center, float range, out Vector3 result)
    {

        Vector3 randomPoint = center + Random.insideUnitSphere * range; //random point in a sphere
        NavMeshHit hit;
        if (NavMesh.SamplePosition(randomPoint, out hit, 1.0f, NavMesh.AllAreas)) //documentation: https://docs.unity3d.com/ScriptReference/AI.NavMesh.SamplePosition.html
        {
            //the 1.0f is the max distance from the random point to a point on the navmesh, might want to increase if range is big
            //or add a for loop like in the documentation
            result = hit.position;
            return true;
        }

        result = Vector3.zero;
        return false;
    }

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74595411

复制
相关文章

相似问题

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