内容不多, 一个脚本, 直接上代码
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Move : MonoBehaviour
- {
- float speed = 20;
- public float distance_v;
- public float distance_h;
- public float rotation_H_speed = 1;
- public float rotation_V_speed = 1;
- public float max_up_angle = 80; // 越大, 头抬得越高
- public float max_down_angle = -60; // 越小, 头抬得越低
- private float current_rotation_H; // 水平旋转结果
- private float current_rotation_V; // 垂直旋转结果
- void LateUpdate()
- {
- // 旋转
- if (Input.GetMouseButton(1))
- {
- // 控制旋转
- current_rotation_H += Input.GetAxis("Mouse X") * rotation_H_speed;
- current_rotation_V += Input.GetAxis("Mouse Y") * rotation_V_speed;
- //current_rotation_V = Mathf.Clamp(current_rotation_V, max_down_angle, max_up_angle); // 限制垂直旋转角度
- transform.localEulerAngles = new Vector3(-current_rotation_V, current_rotation_H, 0f);
- transform.Translate(Vector3.back * distance_h, Space.Self);
- transform.Translate(Vector3.up * distance_v, Space.World); // 相对于世界坐标 y 轴向上
- }
- // 平移
- if (Input.GetMouseButton(2))
- {
- this.transform.Translate(new Vector3(-Input.GetAxis("Mouse X") * rotation_H_speed, -Input.GetAxis("Mouse Y") * rotation_V_speed, 0f));
- }
- }
- // Use this for initialization
- void Start()
- {
- }
- // Update is called once per frame
- void Update()
- {
- // 移动
- if (Input.GetKey(KeyCode.A)) // 左移
- {
- transform.Translate(Vector3.left * speed * Time.deltaTime);
- }
- if (Input.GetKey(KeyCode.D)) // 右移
- {
- transform.Translate(Vector3.right * speed * Time.deltaTime);
- }
- if (Input.GetKey(KeyCode.W)) // 前移
- {
- transform.Translate(Vector3.forward * speed * Time.deltaTime);
- }
- if (Input.GetKey(KeyCode.S)) // 后移
- {
- transform.Translate(Vector3.back * speed * Time.deltaTime);
- }
- // 缩放
- if (Input.GetAxis("Mouse ScrollWheel") != 0)
- {
- // 获取鼠标滚轮的滑动量
- float wheel = Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * 5000;
- // 改变相机的位置
- this.transform.Translate(Vector3.forward * wheel);
- }
- }
- }
- Camera
来源: http://www.bubuko.com/infodetail-3064181.html