更换用户基类
parent
e2a0db671b
commit
1187e4012e
|
@ -0,0 +1,169 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -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<CharacterController>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue