Unity 相机平移、旋转、缩放

内容不多,一个脚本,直接上代码

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 
 5 public class Move : MonoBehaviour
 6 {
 7 
 8     float speed = 20;
 9     public float distance_v;
10     public float distance_h;
11     public float rotation_H_speed = 1;
12     public float rotation_V_speed = 1;
13     public float max_up_angle = 80;              //越大,头抬得越高
14     public float max_down_angle = -60;           //越小,头抬得越低
15 
16 
17     private float current_rotation_H;  //水平旋转结果
18     private float current_rotation_V;  //垂直旋转结果
19     void LateUpdate()
20     {
21         // 旋转
22         if (Input.GetMouseButton(1))
23         {
24             //控制旋转
25             current_rotation_H += Input.GetAxis("Mouse X") * rotation_H_speed;
26             current_rotation_V += Input.GetAxis("Mouse Y") * rotation_V_speed;
27             //current_rotation_V = Mathf.Clamp(current_rotation_V, max_down_angle, max_up_angle);       //限制垂直旋转角度
28             transform.localEulerAngles = new Vector3(-current_rotation_V, current_rotation_H, 0f);
29             transform.Translate(Vector3.back * distance_h, Space.Self);
30             transform.Translate(Vector3.up * distance_v, Space.World);          //相对于世界坐标y轴向上
31         }
32 
33         // 平移
34         if (Input.GetMouseButton(2))
35         {
36             this.transform.Translate(new Vector3(-Input.GetAxis("Mouse X") * rotation_H_speed, -Input.GetAxis("Mouse Y") * rotation_V_speed, 0f));
37         }
38     }
39     // Use this for initialization
40     void Start()
41     {
42 
43     }
44 
45     // Update is called once per frame
46     void Update()
47     {
48         // 移动
49         if (Input.GetKey(KeyCode.A)) //左移
50         {
51             transform.Translate(Vector3.left * speed * Time.deltaTime);
52         }
53         if (Input.GetKey(KeyCode.D)) //右移
54         {
55             transform.Translate(Vector3.right * speed * Time.deltaTime);
56 
57         }
58         if (Input.GetKey(KeyCode.W)) //前移
59         {
60             transform.Translate(Vector3.forward * speed * Time.deltaTime);
61 
62         }
63         if (Input.GetKey(KeyCode.S)) //后移
64         {
65             transform.Translate(Vector3.back * speed * Time.deltaTime);
66 
67         }
68 
69         // 缩放
70         if (Input.GetAxis("Mouse ScrollWheel") != 0)
71         {
72             //获取鼠标滚轮的滑动量
73             float wheel = Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * 5000;
74 
75             //改变相机的位置
76             this.transform.Translate(Vector3.forward * wheel);
77         }
78     }
79 }
Camera
原文地址:https://www.cnblogs.com/huojiaoqingchun0123/p/10888550.html