简单的镜头移动脚本
2019-04-10 15:56
99 查看
可以直接放镜头或gameobject上,适合观看场景
Version 3:基于Version2功能上简化了操作
using System.Collections; using System.Collections.Generic; using UnityEngine; public class MOVE : MonoBehaviour { public Transform leftRotateCenter; public Transform rightRotateCenter; private Transform currentRotateCenter; public float speedLevel = 1; private float mySpeed; private void Update() { if (Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(1)) { currentRotateCenter = null; mySpeed = 0; } if (Input.GetMouseButtonDown(0)) { currentRotateCenter = leftRotateCenter; mySpeed = -100; } if (Input.GetMouseButtonDown(1)) { currentRotateCenter = rightRotateCenter; mySpeed = 100; } if (Input.GetKeyDown("-") && speedLevel > 0.5) { speedLevel -= 0.5f; } if (Input.GetKeyDown("=") && speedLevel < 3) { speedLevel += 0.5f; } } private void LateUpdate() { if (currentRotateCenter != null) { this.transform.RotateAround(currentRotateCenter.position, Vector3.up, mySpeed * speedLevel * Time.deltaTime); } else if (Input.GetAxis("Mouse ScrollWheel") != 0) { if (Input.GetKey("space")) { transform.Translate(0, Input.GetAxis("Mouse ScrollWheel") * speedLevel, 0); } else { transform.Translate(0, 0, Input.GetAxis("Mouse ScrollWheel") * speedLevel); } } } }
Version 2:支持多种速度、分开了shift 和 space 的功能。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class MOVE: MonoBehaviour { public float horiSpeed = 10f; public float vertSpeed = 3f; public float speed = 2f; public float maxSpeed = 4f; public float minSpeed = 1f; private void Update() { if (Input.GetKeyUp("=") && speed < maxSpeed) { speed += 1; } if (Input.GetKeyUp("-") && speed > minSpeed) { speed -= 1; } } private void LateUpdate() { if (Input.GetKey("left shift")) { var turnArround = Input.GetAxis("Horizontal") * Time.deltaTime * horiSpeed * speed; var horizontal = Input.GetAxis("Vertical") * Time.deltaTime * horiSpeed * (speed - 0.9f); transform.Rotate(0, turnArround, 0); transform.Translate(horizontal, 0, 0); } else if (Input.GetKey("space")) { var jump = Input.GetAxis("Vertical") * Time.deltaTime * horiSpeed * 2 ; transform.Translate(0, jump, 0); } else { var horizontal = Input.GetAxis("Vertical") * Time.deltaTime * horiSpeed * speed; var vertical = Input.GetAxis("Horizontal") * Time.deltaTime * horiSpeed * speed; transform.Translate(horizontal, 0, vertical); } } }
Version 1:space 功能是升降,shift 功能是减速
using System.Collections; using System.Collections.Generic; using UnityEngine; public class MOVE : MonoBehaviour { public float fly = 10f; public float Vert = 20.0f; public float Hori = 100f; void Update() { if (Input.GetKeyUp("left shift")) { Vert = 20f; Hori = 100f; fly = 10f; } if (Input.GetKeyDown("left shift")) { Vert = 4f; Hori = 20f; fly = 2f; } if (Input.GetKey("space")) { var y = Input.GetAxis("Horizontal") * Time.deltaTime * Hori; var x = Input.GetAxis("Vertical") * Time.deltaTime * Vert; transform.Translate(0, x, 0); transform.Rotate(0, y, 0); } else { var y = Input.GetAxis("Horizontal") * Time.deltaTime * Hori; var z = Input.GetAxis("Vertical") * Time.deltaTime * Vert; transform.Translate(y, 0, z); } }
相关文章推荐
- unity。。。完美简单解决靠鼠标在屏幕上的移动来控制相机水平,垂直旋转的c#脚本
- 简单的移动设备检测PHP脚本
- 简单的移动设备检测PHP脚本代码
- [Unity3D]手机3D游戏开发:简单的移动和缩放鼠标手势脚本源码
- 简单的移动设备检测PHP脚本代码
- unity 3D学习日记:创建一个小场景并编写简单C#移动脚本
- JavaScript判断手机号运营商是移动、联通、电信还是其他(代码简单
- 天池离线赛 - 移动推荐算法(二):基于简单规则的预测
- 一个简单的脚本让你的ubuntu14.04记忆屏幕亮度
- Unity3D人物控制移动脚本编写及分析
- dhcp配置简单脚本
- 简单脚本处理内存溢出
- 截取简单SQL脚本中使用的表及字段
- 简单快速地隐藏移动硬盘上的文件
- [Unity3D]简单的UGUI滑条控制物体移动
- 使用GNS3与SecureCRT关联自动导入脚本(最简单方法)
- RF框架-一个简单的脚本实现及元素定位不到问题的解决
- 简约而不简单,谈一谈移动开发中的那些极简设计
- 恶意移动代码------浏览器脚本
- shell脚本----简单日记系统