1.工程里导入下载好的人物模型

如果出现人物颜色为黑色,可以调整其材质,黑色部分改为白色。

2.烘培(修改路面为可行走)。

把Navigation窗口打开,

选中地面(需要行走的地方),勾选Navigation Ststic,选择Navigation Area为Walkable,点击Bake。

3.实现人物模型在路面上行走。

(没有这一步人物也可以在路面行走,但是需要通过鼠标或者键盘来操控,如果需要完成自动寻路功能的话,就必须使用)

选中人物,选择Nav Mesh Agent。

新建一个C#脚本womenRun,代码如下:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;public class run : MonoBehaviour {private NavMeshAgent agent;void CameraRotateToLookAt(Transform target, float rotateSpeed){//取得目标物体相对于相机的法向量Vector3 normalize = Vector3.Cross(this.transform.forward, target.transform.position - this.transform.position);float angles = Vector3.Angle(this.transform.position, target.transform.position);//以该法向量为轴进行旋转this.transform.Rotate(normalize, Time.deltaTime * rotateSpeed, Space.Self);Debug.Log(angles);if (angles == 0){rotateSpeed = 0;}}// Use this for initializationvoid Start () {agent = GetComponent<NavMeshAgent>();}// Update is called once per framevoid Update () {if (Input.GetMouseButtonDown(0)){Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);RaycastHit hit;if (Physics.Raycast(ray, out hit)){Vector3 point = hit.point;//                transform.LookAt (new Vector3 (point.x, transform.position.y, point.z));CameraRotateToLookAt(hit.transform, 1.0f);}agent.SetDestination(hit.point);}}
}

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

public class womenRun : MonoBehaviour
{

private NavMeshAgent agent;

void CameraRotateToLookAt(Transform target, float rotateSpeed)
    {
        //取得目标物体相对于相机的法向量
        Vector3 normalize = Vector3.Cross(this.transform.forward, target.transform.position - this.transform.position);
        float angles = Vector3.Angle(this.transform.position, target.transform.position);

//以该法向量为轴进行旋转
        this.transform.Rotate(normalize, Time.deltaTime * rotateSpeed, Space.Self);
        Debug.Log(angles);
        if (angles == 0)
        {
            rotateSpeed = 0;
        }
    }

void Start()
    {
        agent = GetComponent<NavMeshAgent>();

}

// Update is called once per frame
    void Update()
    {

if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {

Vector3 point = hit.point;
                //                transform.LookAt (new Vector3 (point.x, transform.position.y, point.z));
                CameraRotateToLookAt(hit.transform, 1.0f);

}
            agent.SetDestination(hit.point);

}

}
}

把脚本挂在人物身上,即完成鼠标点击位置,人物可以自己走过去。

4.增加人物动画

在1位置上选中需要添加动画的模型;

在2位置上添加一个Take001;

在3位置上改变名字,并在4位置上调整时间;

在5位置上勾选LoopTime。(如果不勾选,人物动作不能重复)

完成如图:

新建人物控制器:

双击进入Animator Controller,新建2个空状态,修改其名称,对应添加motion。(先)

场景人物的Animator组件中控制器选中之前新建的人物控制器,并勾选Apply Root Motion。

修改脚本代码如下:(红色部分为新添加的)

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

public class womenRun : MonoBehaviour
{

private NavMeshAgent agent;
    private Animator animator;

void CameraRotateToLookAt(Transform target, float rotateSpeed)
    {
        //取得目标物体相对于相机的法向量
        Vector3 normalize = Vector3.Cross(this.transform.forward, target.transform.position - this.transform.position);
        float angles = Vector3.Angle(this.transform.position, target.transform.position);

//以该法向量为轴进行旋转
        this.transform.Rotate(normalize, Time.deltaTime * rotateSpeed, Space.Self);
        Debug.Log(angles);
        if (angles == 0)
        {
            rotateSpeed = 0;
        }
    }

void Start()
    {
        agent = GetComponent<NavMeshAgent>();
        animator = GetComponent<Animator>();

}

// Update is called once per frame
    void Update()
    {

if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {

Vector3 point = hit.point;
                //                transform.LookAt (new Vector3 (point.x, transform.position.y, point.z));
                CameraRotateToLookAt(hit.transform, 1.0f);

}
            agent.SetDestination(hit.point);
            animator.Play("run");

}

}
}

Unity3d任务模型自动寻路(人员疏散)相关推荐

  1. Unity3D普通开发人员,主程分别需要掌握的技能

    Unity3D普通开发人员 1.会查看和搜索unity API文档,熟悉一些3D术语单词 2.查看别人的代码,能够依葫芦画瓢 3.能够制作一些常见的UI效果,善用缓动类插件,如Do Tween 4.学 ...

  2. 【Unity3D】自动寻路系统Navigation实现人物上楼梯、走斜坡、攀爬、跳跃

    @toc 参考文章:列表 Unity3D深入浅出 - 导航网格自动寻路(Navigation Mesh) unity3D--自带寻路Navmesh入门教程(二) Unity3D自动寻路系统Naviga ...

  3. 基于Unity3D的自动寻路车辆模拟

    案例展示 1.开发背景 仿真测试可以在开发的早期,在即便实际处理器控制器和实车都没有ready的情况下--依然能对算法进行测试,尽早发现bug,尽早解决bug,而不把过多的bug带到实车测试中去. 2 ...

  4. 基于元胞自动机的人员疏散模拟器研究(Matlab代码实现)

  5. 【元胞自动机】基于元胞自动机模拟考虑心理策略的人员疏散附matlab代码

    ✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可私信.

  6. matlab仿真疏散,276基于matlab的疏散仿真程序简介

    基于matlab的疏散仿真程序简介 朱伟向大海刘方 重庆大学城市建设与环境工程学院 400045 摘要:本文简要介绍了人员疏散仿真现状,作者基于matlab平台开发了疏散仿真模型,该仿真模型基于精细网 ...

  7. 版权监控中心怎么关闭_防火门监控系统让消防通道疏散更安全

    一.什么是防火门监控系统 防火门监控系统是显示及控制防火门打开.关闭状态的控制装置,同时做为中心控制室连接前端电动闭门器.门磁开关.电磁释放器.逃生门锁等装置的桥梁和纽带.在防火门监控器故障报警后,精 ...

  8. 地面指示标志_详解消防应急照明和疏散指示系统的施工及安装方法消防施工

    详解消防应急照明和疏散指示系统的施工及安装方法消防施工 消防应急照明与疏散指示系统在生活中随处可见,相信您并不陌生,它是为人员疏散.消防作业提供照明和疏散指示的系统,由各类消防应急灯具及相关装置组成. ...

  9. 地面指示标志_消防应急照明与疏散指示系统的设计和施工安装规范,收藏学习!...

    消防应急照明和疏散指示系统是由各类消防应急灯具及相关装组成,为人员疏散.消防作业提供照明和疏散指示的系统.民用建筑中应设疏散照明的部位在GB50016-2014<建筑设计防火规范>第10. ...

最新文章

  1. 总点第一个视频产生选择偏差?Youtube用“浅塔”来纠正
  2. Tensor Decomposition
  3. fastadmin 后台管理 时间戳字段使用
  4. Java集合源码学习(4)HashSet
  5. [POI2014]Freight
  6. 线性代数-线性转化和矩阵
  7. window核心编程 22.3 DIPS.exe的踩坑逻辑梳理
  8. 【嵌入式技术】Atmega128串口详解
  9. 关于python通过pyautocad操作cad的2021-06-10
  10. 如何制作一首歌的歌词 LRC 歌词制作和绑定
  11. python网格交易法详解_期货交易技巧:等分网格交易法详解
  12. 整理研究方向(1)【未完成】
  13. Boardcast Receiver 源码分析:广播的动态注册、发送和接收过程
  14. python dataframe mean_Python之DataFrame数据处理
  15. TensorFlow的几点小知识
  16. 计算机网络多项式的定义,多项式的定义与概念
  17. 如何进入他人计算机硬盘,手机如何访问电脑硬盘
  18. hihocoder1082 然而沼跃鱼早就看穿了一切
  19. 有趣的CSS实现“勾号”
  20. msvcp120 mysql_Mysql 安装问题一:无法启动此程序,因为计算机丢失MSVCP120.dll,MSVCR120.dll...

热门文章

  1. AI伦理专家成立一个新的人工智能(AI)研究所 提供不同观点
  2. JDK Stream
  3. nlp gpt论文_gpt 3变形金刚和nlp的狂野世界
  4. HTML基础---01
  5. java BufferedImage 转base64
  6. 关于一个CUE文件及多个分轨APE合并成CUE及APE文件的办法
  7. 你知道CAD软件将圆弧与直线转换成多段线的方法吗?
  8. 升级Windows 10 22H2的五种方法
  9. Linux安装Kibana详细教程
  10. 使用SDL2_mixer库播放MP3音乐