Unity>物体的基本属性地形绘制、模块、刚体、碰撞、碰撞检测函数、灯光烘焙、灯光闪烁动画、导航系统、获得鼠标点击坐标、角色移动到鼠标位置、角色动画播放

tech2025-11-20  1

Unity常用部件/功能

名称英文功能地面Terrain场景的地面,使用Terrain模块中的小工具可以进行地形的绘制刚体Rigidbody该模块为物体增加刚体属性,可以通过调节物体的重量摩擦力调整物体的运动轨迹碰撞Collider为物体增加碰撞属性(不同的碰撞器效果是不一样的,可以更换,不同碰撞器消耗性能也是不一样的box collider性能消耗最小Mesh collider性能消耗最大),想要发生碰撞两个碰撞物体必须都要有collider,并且至少有一个Rigidbody碰撞检测函数一个脚本文件检测与其他物体的碰撞

碰撞检测函数

//Collision one call at a time private void OnCollisionEnter(Collision collision) { print("OnCollisionEnterr"); } //collision left once,called once private void OnCollisionExit(Collision collision) { print("OnCollisionExit"); } //Continuous call on collision private void OnCollisionStay(Collision collision) { print("OnCollisionStay"); }

写好了脚本直接将脚本拖拽添加到物体属性栏就可以了

碰撞信息的获取

private void OnCllisionEnter(Collision collision) { print(collision.collider);//与之碰撞物体控制器 print(collision.collider.name);//获取与之碰撞物体的名字 tag标签 }

触发检测

勾选触发器选项,物体就从碰撞器改为触发器了

private void OnTriggerEnter(Collision collision) {} private void OnTriggerExit(Collision collision) {} private void OnTriggerStay(Collision collision) {}

Lightmapping(光照烘焙)

光照贴图可以极大地提高游戏性能 1.打开windows>Light>setting窗口 2.光源模式修改为Baked(烘焙后后期无法改变)或者Mixed(混合的) 3.将地形/建筑物勾选为静态 4.点击生成烘焙

生成粒子效果的火焰

1.在光源处添加粒子系统 2.调整粒子系统参数,使之接近真实火焰粒子效果 3.创建粒子贴图 准备4x4的动画贴图 新建一个材料Material,选择Particles/Additive,选择贴图和白色背景 选择粒子特效里的Render,Material选择刚刚制作的material 动画效果修改为4x4贴图

灯光的闪烁动画

1.选中灯光 2.选择windows>Animation,Create创建保存动画 3.选择Add Property>Light>选择Intensity或者color最为变量创建动画

导航系统

1.windows>Navigation 2.将地形和背景标记为Navigation static 3.Bake烘焙生成导航网格 4.若有的背景不应在上面移动则选中背景物体Navigation>Object>选择Not Walkable 5.为人物添加导航组件Nav Mesh Agent

返回鼠标点击坐标

// Update is called once per frame void Update () { if (Input.GetMouseButtonDown(0))//if mouse left down { Ray ray; ray= Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit))//initialize hit { print(hit.point);//hit.point } } }

如果Camera.main.ScreenPointToRay(Input.mousePosition);报错空引用是由于camera的tag没有设置为MainCamera

角色移动到鼠标位置

using UnityEngine.AI;//add this namespace public class Hero : MonoBehaviour { public NavMeshAgent agent; //make a public agent ,it used to save NavMehAgent`s information void Update () { if (Input.GetMouseButtonDown(0))//if mouse left down { Ray ray; ray= Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit))//initialize hit { // print(hit.point);//hit.point agent.SetDestination(hit.point);//set target location } } } }

public对象直接拖拽赋值就可以了

镜头跟随角色移动

为camera编写脚本

public class CamerMove : MonoBehaviour { public Transform hero;//create target to get hero location private Vector3 shift;//hoer whit camera // Use this for initialization void Start () { shift = transform.position - hero.position; } // Update is called once per frame void Update () { transform.position = hero.position + shift; } }

角色动画播放

1.角色有Animator组件,有contorller对动画进行控制 2.在动画文件夹下创建Animator Contorller为上述组件contorller赋值 3.编辑Animator Contorller (1)添加视频文件,构建关系 (2)添加动画转变判断条件 (3)进行动画转变条件设置 4.修改hero脚本,传递speed参数

public NavMeshAgent agent;//get nav mash agent public Animator anim;//get animator void Update() { anim.setFloat("speed",agent.velocity.magenitude);//set speed }
最新回复(0)