using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { public GameObject CapsulePlayer, MainCamerahead; private float aixsX, aixsY, mouseScroll; private int moveSpeed; private int standardMoveSpeed = 600; private int runMoveSpeed = 1000; private int mouseScrollSpeed = 10; private float mouseScrolbackSpeed = 0.05f; private CharacterController charaControl; private Camera charaCam; public float mouseNumOne = 0, mouseNumTwo = 0; public static bool chufa = false; void Awake() { charaControl = GetComponent<CharacterController>(); charaCam = MainCamerahead.GetComponent<Camera>(); } void Update() { aixsX = Input.GetAxis("Horizontal"); aixsY = Input.GetAxis("Vertical"); mouseScroll = Input.GetAxis("Mouse ScrollWheel"); if (Input.GetKey(KeyCode.LeftShift)) { moveSpeed = runMoveSpeed; } else { moveSpeed = standardMoveSpeed; } charaControl.SimpleMove(transform.forward * moveSpeed * aixsY * 0.02f + transform.right * moveSpeed * aixsX * 0.02f); /* ***********************镜头跟随鼠标移动的第一种方法********************** float mousex = Input.GetAxis("Mouse X") * 2f; float mousey = Input.GetAxis("Mouse Y") * 2f; if (mousex > 180) { mousex = 180; } else if (mousex < -180) { mousex = -180; } if (Mathf.Abs(mousey) < 120f) { MainCamerahead.transform.localRotation = charaCam.transform.localRotation * Quaternion.Euler(-mousey, 0, 0); CapsulePlayer.transform.localRotation = transform.localRotation * Quaternion.Euler(0, mousex, 0); } */ //镜头跟随鼠标移动的第二种写法 (总之写法有很多,自己用着舒服就行) if (chufa ) { mouseNumOne += Input.GetAxis("Mouse Y"); mouseNumTwo += Input.GetAxis("Mouse X"); if (mouseNumOne > 180) { mouseNumOne = 180; } else if (mouseNumOne < -180) { mouseNumOne = -180; } if (Mathf.Abs(mouseNumOne) < 90f) { MainCamerahead.transform.Rotate(new Vector3(-1 * Input.GetAxis("Mouse Y"), 0, 0)); CapsulePlayer.transform.Rotate(0, 1.6f * Input.GetAxis("Mouse X"), 0); } } else { //鼠标滑动镜头跟着滑动 if (Input.GetMouseButton(0)) { mouseNumOne += Input.GetAxis("Mouse Y"); mouseNumTwo += Input.GetAxis("Mouse X"); if (mouseNumOne > 180) { mouseNumOne = 180; } else if (mouseNumOne < -180) { mouseNumOne = -180; } if (Mathf.Abs(mouseNumOne) < 90f) { MainCamerahead.transform.Rotate(new Vector3(-1 * Input.GetAxis("Mouse Y"), 0, 0)); CapsulePlayer.transform.Rotate(0, 1.6f * Input.GetAxis("Mouse X"), 0); } } } //放大缩小功能 if (charaCam.fieldOfView > 60) { charaCam.fieldOfView -= mouseScrolbackSpeed; } else if (charaCam.fieldOfView < 10) { charaCam.fieldOfView += mouseScrolbackSpeed; } else { charaCam.fieldOfView -= mouseScroll * mouseScrollSpeed; } } }
Demo链接:https://pan.baidu.com/s/1s8At3pMXI94-6Ri14_JvPw
提取码:1cbw
代码写的不好,大家多多包涵。