diff --git a/Assets/PlayerBase.cs b/Assets/PlayerBase.cs new file mode 100644 index 0000000..a629658 --- /dev/null +++ b/Assets/PlayerBase.cs @@ -0,0 +1,169 @@ +using UnityEngine; + +namespace ZFramework +{ + /// + /// 用户基类 + /// + [RequireComponent(typeof(CharacterController))] + public class PlayerBase : MonoBehaviour + { + /// + /// 角色控制器 + /// + CharacterController characterController; + + /// + /// 方向 + /// + Vector3 direction; + + /// + /// 移动速度 + /// + [SerializeField] protected float moveSpeed = 5; + + /// + /// 跳跃高度 + /// + protected float jumpPower = 5; + + /// + /// 重力 + /// + [SerializeField] protected float gravity = 9.8f; + + /// + /// 旋转速度 + /// + protected float mouseSpeed = 5; + + /// + /// 最小视角角度 + /// + protected float minmouseY = -45; + + /// + /// 最大视角角度 + /// + protected float maxmouseY = 45; + + /// + /// 是否能移动 + /// + [SerializeField] protected bool isCanMove = true; + + /// + /// 是否能旋转 + /// + [SerializeField] protected bool isCanRotate = true; + + /// + /// 是否能跳跃 + /// + [SerializeField] protected bool isCanJump = true; + + /// + /// 是否暂停玩家操控 + /// + private bool isPause; + + float RotationY = 0f; + float RotationX = 0f; + + + /// + /// 摄像机(用户视角) + /// + private Transform mainCamera; + + float _horizontal; + float _vertical; + + private void Awake() + { + OnAwake(); + } + + private void FixedUpdate() + { + if (isPause) return; + + _horizontal = Input.GetAxis("Horizontal"); + _vertical = Input.GetAxis("Vertical"); + OnFixedUpdate(); + } + + public virtual void OnAwake() + { + isPause = false; + characterController = GetComponent(); + mainCamera = transform.Find("MainCamera"); + if (mainCamera == null) Debug.LogError("transform find not have mainCamera! please check!"); + } + + public virtual void OnFixedUpdate() + { + PlayerMove(); + CameraRotate(); + } + + /// + /// 暂停玩家控制(此处的暂停包括玩家的所有控制) + /// + public virtual void Pause() + { + isPause = true; + } + + /// + /// 恢复玩家控制(此处的暂停包括玩家的所有控制) + /// + public virtual void Resume() + { + isPause = false; + } + + /// + /// 用户移动控制 + /// + void PlayerMove() + { + if (isCanMove) + { + if (characterController.isGrounded) + { + direction = new Vector3(_horizontal, 0, _vertical); + if (Input.GetKeyDown(KeyCode.Space) && isCanJump) + direction.y = jumpPower; + } + + direction.y -= gravity * Time.deltaTime; + characterController.Move( + characterController.transform.TransformDirection(direction * Time.deltaTime * moveSpeed)); + } + } + + /// + /// 相机旋转视角控制 + /// + void CameraRotate() + { + if (isCanRotate) + { + if (Input.GetMouseButton(1)) + { + RotationX += mainCamera.transform.localEulerAngles.y + Input.GetAxis("Mouse X") * mouseSpeed; + + RotationY -= Input.GetAxis("Mouse Y") * mouseSpeed; + RotationY = Mathf.Clamp(RotationY, minmouseY, maxmouseY); + + this.transform.eulerAngles = new Vector3(0, RotationX, 0); + + mainCamera.transform.eulerAngles = new Vector3(RotationY, RotationX, 0); + } + } + } + + } +} \ No newline at end of file diff --git a/Assets/PlayerMove.cs.meta b/Assets/PlayerBase.cs.meta similarity index 100% rename from Assets/PlayerMove.cs.meta rename to Assets/PlayerBase.cs.meta diff --git a/Assets/PlayerMove.cs b/Assets/PlayerMove.cs deleted file mode 100644 index 30dee7c..0000000 --- a/Assets/PlayerMove.cs +++ /dev/null @@ -1,52 +0,0 @@ -using System; -using UnityEngine; - -[RequireComponent(typeof(CharacterController))] -public class PlayerMove : MonoBehaviour -{ - private CharacterController _controller; - [SerializeField] private float moveSpeed = 6.0f; - - private Camera mainCamera; - [SerializeField] private float rotateSpeed = 50; - private float x, y; - - //重力 - [SerializeField] private float gravity = 110f; - [SerializeField] private float jumpSpeed = 8.0f; - private Vector3 moveDirection = Vector3.zero; - - private void Awake() - { - _controller = GetComponent(); - mainCamera = Camera.main; - - } - - private void Update() - { - // Move - if (_controller.isGrounded) - { - moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); - moveDirection = transform.TransformDirection(moveDirection); - moveDirection *= moveSpeed; - if (Input.GetButton("Jump")) - moveDirection.y = jumpSpeed; - } - - moveDirection.y -= gravity * Time.deltaTime; - _controller.Move(moveDirection * Time.deltaTime); - - // Rotate - if (Input.GetMouseButton(1)) - { - y = Input.GetAxis("Mouse X"); - x = Input.GetAxis("Mouse Y"); - mainCamera.transform.eulerAngles += - new Vector3(-x * Time.deltaTime * rotateSpeed, 0, 0); - transform.eulerAngles += - new Vector3(0, y * Time.deltaTime * rotateSpeed, 0); - } - } -} \ No newline at end of file