关于Navigation

在游戏运行时新加入如(或删除)建筑类的游戏物体时

using UnityEngine.AI;
class xx{
//注意!!!NavMeshSurface需要额外下载NavMeshsComponets
    NavMeshSurface nms;
    nms = GetComponent<NavMeshSurface>();
    nms.BuildNavMesh();
}
//如果新加入的游戏物体没有bulid迹象,可能是因为游戏物体的高度没达到AgentType step height!!!

使用NavMeshAgent寻路,但是自己控制移动。

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

public class NavigetAgent : MonoBehaviour {
    //public Transform target;
    bool isColider;
    RaycastHit hit;
    NavMeshAgent agent;
    private void Start()
    {
        agent = GetComponent<NavMeshAgent>();
        agent.updatePosition = false;
        agent.updateRotation = false;//禁止agent控制角色
    }
    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            isColider = Physics.Raycast(ray, out hit);
        }
        if (isColider)
        {
            agent.nextPosition = transform.position;//将agent限制在角色内。我们只需要NavMeshAgent.desiredVlocity参数来取得前进方向即可
            agent.destination=hit.point; 
            isColider = false;  
        }
        if (agent.remainingDistance > 0.1f)
        {
            move();
        }
    }
    void move()
    {
        agent.nextPosition = transform.position;
        transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(agent.desiredVelocity), 20 * Time.deltaTime);//NavMeshAgent.desiredVlocity 期望的速度?我觉得就是agent的前进方向!!!
        transform.Translate(Vector3.forward * 4 * Time.deltaTime);//使用Translate时最好使用Vector3方向参数
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值