Merge pull request 'add:添加player基础类' (#1) from newgame into master

Reviewed-on: #1
pull/3/head
zxl 2024-11-13 16:29:40 +08:00
commit 24368c6aa4
3 changed files with 301 additions and 8 deletions

View File

@ -104,7 +104,7 @@ NavMeshSettings:
serializedVersion: 2
m_ObjectHideFlags: 0
m_BuildSettings:
serializedVersion: 2
serializedVersion: 3
agentTypeID: 0
agentRadius: 0.5
agentHeight: 2
@ -117,7 +117,7 @@ NavMeshSettings:
cellSize: 0.16666667
manualTileSize: 0
tileSize: 256
accuratePlacement: 0
buildHeightMesh: 0
maxJobWorkers: 0
preserveTilesOutsideBounds: 0
debug:
@ -209,14 +209,120 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 913828149}
serializedVersion: 2
m_LocalRotation: {x: 0.40821788, y: -0.23456968, z: 0.10938163, w: 0.8754261}
m_LocalPosition: {x: 0, y: 3, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0}
--- !u!1 &1024067690
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1024067694}
- component: {fileID: 1024067693}
- component: {fileID: 1024067692}
- component: {fileID: 1024067691}
m_Layer: 0
m_Name: Plane
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!64 &1024067691
MeshCollider:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1024067690}
m_Material: {fileID: 0}
m_IncludeLayers:
serializedVersion: 2
m_Bits: 0
m_ExcludeLayers:
serializedVersion: 2
m_Bits: 0
m_LayerOverridePriority: 0
m_IsTrigger: 0
m_ProvidesContacts: 0
m_Enabled: 1
serializedVersion: 5
m_Convex: 0
m_CookingOptions: 30
m_Mesh: {fileID: 10209, guid: 0000000000000000e000000000000000, type: 0}
--- !u!23 &1024067692
MeshRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1024067690}
m_Enabled: 1
m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
m_StaticShadowCaster: 0
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RayTracingMode: 2
m_RayTraceProcedural: 0
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:
- {fileID: 10303, guid: 0000000000000000f000000000000000, type: 0}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_ReceiveGI: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 1
m_SelectedEditorRenderState: 3
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
m_AdditionalVertexStreams: {fileID: 0}
--- !u!33 &1024067693
MeshFilter:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1024067690}
m_Mesh: {fileID: 10209, guid: 0000000000000000e000000000000000, type: 0}
--- !u!4 &1024067694
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1024067690}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 5, y: 5, z: 5}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1301447167
GameObject:
m_ObjectHideFlags: 0
@ -248,9 +354,17 @@ Camera:
m_projectionMatrixMode: 1
m_GateFitMode: 2
m_FOVAxisMode: 0
m_Iso: 200
m_ShutterSpeed: 0.005
m_Aperture: 16
m_FocusDistance: 10
m_FocalLength: 50
m_BladeCount: 5
m_Curvature: {x: 2, y: 11}
m_BarrelClipping: 0.25
m_Anamorphism: 0
m_SensorSize: {x: 36, y: 24}
m_LensShift: {x: 0, y: 0}
m_FocalLength: 50
m_NormalizedViewPortRect:
serializedVersion: 2
x: 0
@ -284,11 +398,18 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1301447167}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 1, z: -10}
serializedVersion: 2
m_LocalRotation: {x: 0.30883646, y: -0, z: -0, w: 0.9511152}
m_LocalPosition: {x: 0, y: 11.6, z: -31.4}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_LocalEulerAnglesHint: {x: 35.978, y: 0, z: 0}
--- !u!1660057539 &9223372036854775807
SceneRoots:
m_ObjectHideFlags: 0
m_Roots:
- {fileID: 1301447170}
- {fileID: 913828151}
- {fileID: 1024067694}

View File

@ -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);
}
}
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: cc965ce1e55644dab9aa4176ccaa6014
timeCreated: 1731482358