169 lines
4.3 KiB
C#
169 lines
4.3 KiB
C#
using UnityEngine;
|
|
|
|
namespace ZFramework
|
|
{
|
|
/// <summary>
|
|
/// 用户基类
|
|
/// </summary>
|
|
[RequireComponent(typeof(CharacterController))]
|
|
public class PlayerBase : MonoBehaviour
|
|
{
|
|
/// <summary>
|
|
/// 角色控制器
|
|
/// </summary>
|
|
CharacterController characterController;
|
|
|
|
/// <summary>
|
|
/// 方向
|
|
/// </summary>
|
|
Vector3 direction;
|
|
|
|
/// <summary>
|
|
/// 移动速度
|
|
/// </summary>
|
|
[SerializeField] protected float moveSpeed = 5;
|
|
|
|
/// <summary>
|
|
/// 跳跃高度
|
|
/// </summary>
|
|
protected float jumpPower = 5;
|
|
|
|
/// <summary>
|
|
/// 重力
|
|
/// </summary>
|
|
[SerializeField] protected float gravity = 9.8f;
|
|
|
|
/// <summary>
|
|
/// 旋转速度
|
|
/// </summary>
|
|
protected float mouseSpeed = 5;
|
|
|
|
/// <summary>
|
|
/// 最小视角角度
|
|
/// </summary>
|
|
protected float minmouseY = -45;
|
|
|
|
/// <summary>
|
|
/// 最大视角角度
|
|
/// </summary>
|
|
protected float maxmouseY = 45;
|
|
|
|
/// <summary>
|
|
/// 是否能移动
|
|
/// </summary>
|
|
[SerializeField] protected bool isCanMove = true;
|
|
|
|
/// <summary>
|
|
/// 是否能旋转
|
|
/// </summary>
|
|
[SerializeField] protected bool isCanRotate = true;
|
|
|
|
/// <summary>
|
|
/// 是否能跳跃
|
|
/// </summary>
|
|
[SerializeField] protected bool isCanJump = true;
|
|
|
|
/// <summary>
|
|
/// 是否暂停玩家操控
|
|
/// </summary>
|
|
private bool isPause;
|
|
|
|
float RotationY = 0f;
|
|
float RotationX = 0f;
|
|
|
|
|
|
/// <summary>
|
|
/// 摄像机(用户视角)
|
|
/// </summary>
|
|
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<CharacterController>();
|
|
mainCamera = transform.Find("MainCamera");
|
|
if (mainCamera == null) Debug.LogError("transform find not have mainCamera! please check!");
|
|
}
|
|
|
|
public virtual void OnFixedUpdate()
|
|
{
|
|
PlayerMove();
|
|
CameraRotate();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 暂停玩家控制(此处的暂停包括玩家的所有控制)
|
|
/// </summary>
|
|
public virtual void Pause()
|
|
{
|
|
isPause = true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 恢复玩家控制(此处的暂停包括玩家的所有控制)
|
|
/// </summary>
|
|
public virtual void Resume()
|
|
{
|
|
isPause = false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 用户移动控制
|
|
/// </summary>
|
|
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));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 相机旋转视角控制
|
|
/// </summary>
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
} |