【问题标题】:Using Nav Mesh Agent to the object in Unity在 Unity 中使用 Nav Mesh Agent 到对象
【发布时间】:2018-04-06 17:16:27
【问题描述】:

我向动画对象添加了一个导航网格代理组件,现在他总是走到 (0,0,0) 位置,而不是跟随玩家。有什么建议吗??

(Unity Survival Shooter 教程!)

using UnityEngine;
using System.Collections;
using UnityEngine.AI;

public class EnemyMovement : MonoBehaviour
{
Transform player;
NavMeshAgent nav;


void Awake ()
{
    player = GameObject.FindGameObjectWithTag ("Player").transform;

    nav = GetComponent <NavMeshAgent> ();
}


void Update ()
{

        nav.SetDestination (player.position);

}
}

【问题讨论】:

  • 尝试添加 "Debug.Log("Name: "+player.name);"将播放器设置为唤醒后,查看控制台中打印的名称是否是预期播放器的名称。还要写"Debug.Log("Position:"+player.position);"然后将结果作为屏幕截图添加到您的帖子中。
  • 嗯,名字是“环境”,位置是(0,0,0)!!这是有道理的))))
  • 我解决了,谢谢!!
  • np,以后你发帖的时候从一开始就更具描述性:-)。

标签: c# unity3d


【解决方案1】:

与 NavMeshAgent 相对?以前是什么让它动起来的?显示使它移动的脚本。您需要在发布问题时添加更多关于您正在做什么的信息。

NavMeshAgent 需要有一个 Vector3 目标集,您通常会从 Transform 中获得该目标集,在这种情况下,如果玩家是玩家 Transform 的目标。

脚本设置例如可以是:

Using UnityEngine.AI;

NavMeshAgent agent;

public float UpdatePositionCooldown = 0.2f;
float UpdatePositionTime;
public Transform Player; //<-- Should be dragged in via inspector in this example

void Awake(){
    agent = GetComponent<NavMeshAgent>();
    UpdatePositionTime = Time.time;
}

void Update(){
    //Set destination if cooldown done.
    if (Time.time > UpdatePositionTime){
    agent.SetDestination(Player.position);
    UpdatePositionTime = UpdatePositionCooldown + Time.time;
    }
}

更多信息和来源:https://docs.unity3d.com/ScriptReference/AI.NavMeshAgent.SetDestination.html

【讨论】:

  • 我添加了移动对象的脚本!
猜你喜欢
  • 1970-01-01
  • 2022-07-09
  • 1970-01-01
  • 1970-01-01
  • 2020-02-01
  • 1970-01-01
  • 2018-08-23
  • 1970-01-01
  • 2021-12-30
相关资源
最近更新 更多