Unity 新导航寻路演示(2)

对于静态场景来说,只需要3步

1.为场景Ground添加网格表面组件并烘焙

2.为player添加导航代理

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class PlayerMove : MonoBehaviour
{
    private NavMeshAgent agent;
    private Animator animator;
    // Start is called before the first frame update
    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
        animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetMouseButton(0))
            { 
            Ray ray =  Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if(Physics.Raycast(ray,out hit,1000))
                {
                agent.SetDestination(hit.point);
            }
        }
        Vector3 v =agent.velocity;
        if(v.magnitude >0.1)
            {
            animator.SetBool("isRunning",true);
        }
        if(v.magnitude < 0.1)
            {
            animator.SetBool("isRunning", false);
        }
    }
}

3.编写代码

相关推荐
csdn_aspnet4 小时前
C# 求n边凸多边形的对角线数量(Find number of diagonals in n sided convex polygon)
开发语言·算法·c#
禹中一只鱼5 小时前
【力扣热题100学习笔记】 - 哈希
java·学习·leetcode·哈希算法
SteveSenna6 小时前
项目:Trossen Arm MuJoCo
人工智能·学习·算法
m0_747304166 小时前
GNN学习
学习
Sagittarius_A*6 小时前
监督学习(Supervised Learning)
人工智能·学习·机器学习·监督学习
风酥糖7 小时前
Godot游戏练习01-第21节-优化游戏菜单,增加选项
游戏·游戏引擎·godot
C蔡博士7 小时前
Unity2D物理系统-从入门到实战优化
unity·游戏引擎·rigidbody2d
qqty12177 小时前
Java进阶学习之路
java·开发语言·学习
WHS-_-20227 小时前
Python 算法题学习笔记一
python·学习·算法
_李小白8 小时前
【OSG学习笔记】Day 22: StateSet 与 StateAttribute (渲染状态)
笔记·学习