unity笔记(脚本3)
键盘按键获取+camera旋转+向量
public class CameraZoom : MonoBehaviour { //同时按下A + B private void Update1() { if (Input.GetKey(KeyCode.A) && Input.GetKeyDown(KeyCode.B)) Debug.Log("按了"); } private Camera camera; private void Start() { //camera = GetComponent<Camera>(); camera = Camera.main;//GameObject.FindWithTag("MainCamera") } private void Update() { Zoom(); } private bool isFar = true; private void Zoom1() { if (Input.GetMouseButtonDown(1)) { //修改缩放等级 isFar = !isFar; if (isFar) { //拉远 20 --》 60 camera.fieldOfView = 60; } else { //拉近 60 --》 20 camera.fieldOfView = 20; } } } private void Zoom2() { if (Input.GetMouseButtonDown(1)) { //修改缩放等级 isFar = !isFar; } if (isFar) { //拉远 20 --》 60 if (camera.fieldOfView < 60) camera.fieldOfView += 2; } else { //拉近 60 --》 20 if (camera.fieldOfView > 20) camera.fieldOfView -= 2; } } private void Zoom3() { if (Input.GetMouseButtonDown(1)) { //修改缩放等级 isFar = !isFar; } if (isFar) { //拉远 20 --》 60 Lerp(起点、终点、比例) camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, 60, 0.1f); //Vector3.Lerp //Quaternion.Lerp //Color.Lerp } else { //拉近 60 --》 20 camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, 20, 0.1f); } } //60 50 40 30 20 public float[] zoomLevels; private int currentLevel; private void Zoom() { if (Input.GetMouseButtonDown(1)) { //修改缩放等级 //currentLevel++; //currentLevel = currentLevel < zoomLevels.Length - 1 ? currentLevel + 1 : 0; currentLevel = (currentLevel + 1) % zoomLevels.Length; } camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, zoomLevels[currentLevel], 0.1f); } }
/// <summary> /// 鼠标控制相机旋转 /// </summary> public class DoRotation : MonoBehaviour { private void Update() { float x = Input.GetAxis("Mouse X"); float y = Input.GetAxis("Mouse Y"); if (x != 0 || y != 0) RotateView(x, y); //需要限制沿X轴旋转角度 } public float speed = 10; private void RotateView(float x, float y) { x *= speed * Time.deltaTime; y *= speed * Time.deltaTime; transform.Rotate(-y, 0, 0); transform.Rotate(0, x, 0, Space.World); } }
public class PlayerController : MonoBehaviour { private void Update() { float hor = Input.GetAxis("Horizontal"); float ver = Input.GetAxis("Vertical"); if (hor != 0 || ver != 0) Movement(hor, ver); } public float moveSpeed = 10; private void Movement(float hor,float ver) { hor *= moveSpeed * Time.deltaTime; ver *= moveSpeed * Time.deltaTime; //限制 Vector3 screentPos = Camera.main.WorldToScreenPoint(transform.position); //如果移动到最下边 并且 还想向下 或者 移动到最上边 并且 还想向上 if (screentPos.y <= 0 && ver < 0 || screentPos.y >= Screen.height && ver >0) ver = 0;//停 this.transform.Translate(hor, 0, ver); } }
/// <summary> /// 向量 /// </summary> public class VectorDemo : MonoBehaviour { public Transform t1, t2,t3; private void Update() { Demo08(); } //计算向量长度 private void Demo01() { Vector3 pos = transform.position; float m1 = Mathf.Sqrt(Mathf.Pow(pos.x, 2) + Mathf.Pow(pos.y, 2) + Mathf.Pow(pos.z, 2)); float m2 = pos.magnitude; print(m1 + "----" + m2); } //计算向量方向 private void Demo02() { Vector3 pos = transform.position; Vector3 n1 = pos / pos.magnitude; Vector3 n2 = pos.normalized; Debug.DrawLine(Vector3.zero, n1, Color.red); } //向量基本运算 private void Demo03() { Vector3 result = t1.position - t2.position; //需求:t3 沿着 result 方向移动 if (Input.GetMouseButtonDown(0)) //t3.Translate(result.normalized); t3.position = t3.position + result.normalized; Debug.DrawLine(Vector3.zero, t1.position); Debug.DrawLine(Vector3.zero, t2.position); Debug.DrawLine(Vector3.zero, result, Color.red); //已知向量a 长度为8.3m //计算该方向 长度为1.5m的向量 //解决方案:将a向量标准化, 再乘以1.5 } //弧度与角度 private void Demo04() { // f1角度=>f2 弧度: 弧度=角度数*PI/180 float f1 = 50; float f2 = f1 * Mathf.PI / 180; float f3 = f1 * Mathf.Deg2Rad; // Mathf.Rad2Deg 弧度 ---> 角度 } //三角函数 private void Demo05() { /* sin x = a / c a = c * sin x; c = a / sin x; */ //已知:角度x 边长 a //计算:边长 c float x = 50; float a = 10; float c = a / Mathf.Sin(x * Mathf.Deg2Rad); //已知:边长 a c //计算:角度angle float angle = Mathf.Asin(a / c) * Mathf.Rad2Deg; print(angle); } //自身坐标 --> 世界坐标 private void Demo06() { Vector3 worldPos = transform.TransformPoint(1, 0, 0); Debug.DrawLine(this.transform.position, worldPos); //transform.forward //transform.up //transform.right } //练习 private void Demo07() { //计算:前右方30度 10米处世界坐标 Vector3 localPos = new Vector3 ( 10 * Mathf.Sin(30 * Mathf.Deg2Rad), 0, 10 * Mathf.Cos(30 * Mathf.Deg2Rad) ); Vector3 worldPos = transform.TransformPoint(localPos); Debug.DrawLine(transform.position, worldPos); } public float angle; //向量 向量 private void Demo08() { //向量 + - 向量 //向量 * / 数 //向量 (点乘) (叉乘) 向量 // //参与点乘运算的向量标准化后,结果为夹角的 cos 值 float dot = Vector3.Dot(t1.position.normalized, t2.position.normalized); angle = Mathf.Acos(dot) * Mathf.Rad2Deg; //if (angle > 30)//比较角度 if (dot < 0.866f)//比较cos值 { //如果夹角大于30度则 } Vector3 cross = Vector3.Cross(t1.position, t2.position); if (cross.y <0) { angle = 360 - angle; } Debug.DrawLine(Vector3.zero, cross, Color.red); Debug.DrawLine(Vector3.zero, t1.position); Debug.DrawLine(Vector3.zero, t2.position); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)