123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- using UnityEngine;
-
- public class MouseLook : MonoBehaviour
- {
- public float sensitivity = 100f; // 敏感度
- public float clampAngle = 80f; // 视角限制角度
-
- private float yMinLimit = -80f; // 最小视角限制
- private float yMaxLimit = 80f; // 最大视角限制
-
- private float x = 0f; // 用于存储camera的X角度
- private float y = 0f; // 用于存储camera的Y角度
-
- public float zoomSpeed = 5.0f; // 调整速度
- public float minFOV = 10.0f; // 最小视角
- public float maxFOV = 90.0f; // 最大视角
-
- private Camera cam;
- private float currentFOV;
- void Start()
- {
- // 初始化角度
- y = transform.eulerAngles.y;
- cam = GetComponent<Camera>();
- currentFOV = cam.fieldOfView;
- }
- private float rotateSpeed = 30f;
- private float movespeed = 5;
- void FixedUpdate()
- {
- }
- bool isenter = false;
- public void enter()
- {
- isenter=true;
- }
- public void exit()
- {
- isenter=false;
- }
- public Transform playerBody; // 角色的body,用于确定转向的参照点
- private float xRotation = 0f; // 记录水平旋转角度
- Vector3 downv3=Vector3.zero;
- void Update()
- {
- if(!isenter)
- return;
- // 检查鼠标滚轮是否有移动
- float scrollInput = Input.GetAxis("Mouse ScrollWheel");
- if (scrollInput != 0)
- {
- // 根据滚轮输入调整视角
- currentFOV -= scrollInput * zoomSpeed;
- currentFOV = Mathf.Clamp(currentFOV, minFOV, maxFOV);
- cam.fieldOfView = currentFOV;
- }
- if (Input.GetKey(KeyCode.W))
- {
- transform.Translate(Vector3.forward * Time.deltaTime * speed*10);
- }
- else if (Input.GetKey(KeyCode.S))
- {
- transform.Translate(Vector3.back * Time.deltaTime * speed*10);
- }
- if (Input.GetKey(KeyCode.A))
- {
- transform.Translate(Vector3.left * Time.deltaTime * speed*10);
- }
- else if (Input.GetKey(KeyCode.D))
- {
- transform.Translate(Vector3.right * Time.deltaTime * speed*10);
- }
- if (!Input.GetMouseButton(1)&&!Input.GetMouseButton(0)) return;
- if (Input.GetMouseButton(0) || Input.GetMouseButton(1))
- {
- transform.Rotate(Vector3.up, Input.GetAxis("Mouse X") * speed * 10);
- transform.Rotate(Vector3.left, Input.GetAxis("Mouse Y") * speed * 10);
- transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, 0);
- }
- if (Input.GetKey(KeyCode.Escape))
- {
- transform.position = positionReset;
- transform.eulerAngles = eulerAnglesReset;
- }
- }
- [Range(0.1f, 10f)]
- public float speed = 0.4f;
- Vector3 positionReset, eulerAnglesReset;
- void Awake()
- {
- positionReset = transform.position;
- eulerAnglesReset = transform.eulerAngles;
- }
- }
|