Unity3D 小案例技巧02------移动物体+摄像机跟随+鼠标操作摄像机旋转+缩放
编辑器Unity2021
1.创建场地------Plane
2.创建主物体------Capsule
3.删除默认相机
4.创建物体移动脚本----Shift.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class Shift : MonoBehaviour { //物体坐标对象 private Transform m_transform; //速度 public float m_movSpeed = 10; void Start() { //获取绑定物体的坐标对象 m_transform = GetComponent<Transform>(); } void Update() { Control(); } void Control() { //定义3个值控制移动 float xm = 0, ym = 0, zm = 0; //按键移动 W=上 if (Input.GetKey(KeyCode.W)) { zm += m_movSpeed * Time.deltaTime; } else if (Input.GetKey(KeyCode.S)) // S=下 { zm -= m_movSpeed * Time.deltaTime; } //按键移动 A=左 if (Input.GetKey(KeyCode.A)) { xm -= m_movSpeed * Time.deltaTime; } else if (Input.GetKey(KeyCode.D)) // D=右 { xm += m_movSpeed * Time.deltaTime; } //跳 = C if (Input.GetKey(KeyCode.C)) { ym += m_movSpeed * Time.deltaTime; } //落 = X if (Input.GetKey(KeyCode.X) && m_transform.position.y >= 1) { ym -= m_movSpeed * Time.deltaTime; } //更新物体坐标位置 m_transform.Translate( new Vector3(xm,ym,zm),Space.Self); } } |
5.将脚本拖入主物体下
6.创建空组件4个,再在最下面创建一个摄像机Camera
7.创建脚本-----CameraShift.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class CameraShift : MonoBehaviour { //控制左右旋转 public Transform y_Axis; //控制上下旋转 public Transform x_Axis; //控制左右倾斜 public Transform z_Axis; //控制远近距离 public Transform zoom_Axis; //物体对象 public Transform player; //旋转速度 public float roSpeed = 180; //缩放速度 public float scSpeed = 50; //限定角度 public float limitAngle = 45; //鼠标左右滑动数值、滚动数值 private float hor, ver, scrollView; float x = 0,sc = 10; //是否跟随物体对象 public bool followFlag; //是否控制物体旋转 public bool turnFlag; private void LateUpdate() { //输入获取 hor = Input.GetAxis( "Mouse X" ); ver = Input.GetAxis( "Mouse Y" ); //鼠标滚动数值 scrollView = Input.GetAxis( "Mouse ScrollWheel" ); //左右滑动鼠标 if (hor!=0) { //围绕Y轴旋转,注意Up是本地坐标的位置 y_Axis.Rotate(Vector3.up*roSpeed*hor*Time.deltaTime); } //上下滑动鼠标 if (ver!=0) { //Y轴移动值 x += -ver * Time.deltaTime * roSpeed; //设置鼠标移动范围 x = Mathf.Clamp(x, -limitAngle,limitAngle); //Quaternion.identity:单位旋转 Quaternion q = Quaternion.identity; //Quaternion.Euler:返回一个旋转,它围绕 z 轴旋转 z 度、围绕 x 轴旋转 x 度、围绕 y 轴旋转 y 度 q = Quaternion.Euler( new Vector3(x,x_Axis.eulerAngles.y,x_Axis.eulerAngles.z)); //Quaternion.Lerp:在 a 和 b 之间插入 t,然后对结果进行标准化处理。参数 t 被限制在 [0, 1] 范围内。 //将旋转值赋值给摄像机坐标对象 x_Axis.rotation = Quaternion.Lerp(x_Axis.rotation,q, Time.deltaTime*roSpeed); } //缩放远近 if (scrollView!=0) { //远近移动值 sc -= scrollView * scSpeed; //设置鼠标移动范围 sc = Mathf.Clamp(sc,3,10); //将缩放值赋值给控制远近距离对象 zoom_Axis.transform.localPosition = new Vector3(0,0,-sc); } //跟随物体对象 当指定物体后 if (followFlag&&player!= null ) { //Vector3.Lerp:在两个点之间进行线性插值。 //Y轴方向的坐标同步 y_Axis.position = Vector3.Lerp(y_Axis.position,player.position+Vector3.up, Time.deltaTime * 10f); } //旋转物体对象 当指定物体后 if (followFlag && player != null ) { //旋转值同步 player.transform.forward = new Vector3(transform.forward.x,0,transform.forward.z); } } } |
8.将脚本添加到摄像机组件下
9.将Y X Z Zoom 主物体(Capsule)拖入对应框中
10.勾选跟随和同步
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?