feat(Player): 让玩家可以轻轻的推动物体
This commit is contained in:
@@ -239,6 +239,8 @@ MonoBehaviour:
|
||||
groundMask:
|
||||
serializedVersion: 2
|
||||
m_Bits: 64
|
||||
fallDamageThreshold: 3
|
||||
pushPower: 0.5
|
||||
--- !u!114 &2963170313432786625
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
|
||||
@@ -2639,11 +2639,6 @@ PrefabInstance:
|
||||
propertyPath: m_Name
|
||||
value: Player
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 3748514579879403869, guid: 667aedb0d3f8c9d469819c9ff2b4472b,
|
||||
type: 3}
|
||||
propertyPath: m_RendererIndex
|
||||
value: -1
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 7437893023972897012, guid: 667aedb0d3f8c9d469819c9ff2b4472b,
|
||||
type: 3}
|
||||
propertyPath: m_LocalPosition.x
|
||||
|
||||
@@ -7,40 +7,40 @@ namespace Script.Gameplay.Player
|
||||
{
|
||||
public class PlayerMoveController : MonoBehaviour
|
||||
{
|
||||
[Header("Movement Settings")]
|
||||
[Tooltip("移动速度(米/秒)")]
|
||||
[Header("Movement Settings")] [Tooltip("移动速度(米/秒)")]
|
||||
public float speed = 5f;
|
||||
[Tooltip("跳跃高度(米)")]
|
||||
public float jumpHeight = 2f;
|
||||
[Tooltip("重力加速度(米/秒²)")]
|
||||
public float gravity = -9.81f;
|
||||
|
||||
[Header("Ground Check")]
|
||||
[Tooltip("检测地面的位置(通常为角色脚下)")]
|
||||
[Tooltip("跳跃高度(米)")] public float jumpHeight = 2f;
|
||||
[Tooltip("重力加速度(米/秒²)")] public float gravity = -9.81f;
|
||||
|
||||
[Header("Ground Check")] [Tooltip("检测地面的位置(通常为角色脚下)")]
|
||||
public Transform groundCheck;
|
||||
[Tooltip("地面检测半径")]
|
||||
public float groundDistance = 0.4f;
|
||||
[Tooltip("地面层(LayerMask)")]
|
||||
public LayerMask groundMask;
|
||||
|
||||
[Tooltip("地面检测半径")] public float groundDistance = 0.4f;
|
||||
[Tooltip("地面层(LayerMask)")] public LayerMask groundMask;
|
||||
|
||||
private float initSpeed;
|
||||
|
||||
|
||||
private CharacterController characterController;
|
||||
private Vector3 velocity;
|
||||
private bool isGrounded;
|
||||
private IInputManager inputManager;
|
||||
|
||||
|
||||
private float lastGroundY;
|
||||
private bool wasGroundedLastFrame;
|
||||
[Tooltip("从高处落下时的伤害阈值(米)")]
|
||||
[SerializeField] private float fallDamageThreshold = 3f; //伤害阈值
|
||||
|
||||
[Tooltip("从高处落下时的伤害阈值(米)")] [SerializeField]
|
||||
private float fallDamageThreshold = 3f; //伤害阈值
|
||||
|
||||
[Tooltip("推动物体的力量")] [SerializeField]
|
||||
private float pushPower = 2f;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
characterController = GetComponent<CharacterController>();
|
||||
if (characterController == null)
|
||||
Debug.LogError("PlayerMoveController 需要 CharacterController 组件!");
|
||||
|
||||
|
||||
initSpeed = speed;
|
||||
}
|
||||
|
||||
@@ -76,7 +76,7 @@ namespace Script.Gameplay.Player
|
||||
}
|
||||
|
||||
wasGroundedLastFrame = isCurrentlyGrounded;
|
||||
|
||||
|
||||
// 地面检测
|
||||
if (groundCheck != null)
|
||||
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
|
||||
@@ -106,11 +106,25 @@ namespace Script.Gameplay.Player
|
||||
characterController.Move(velocity * Time.deltaTime);
|
||||
}
|
||||
|
||||
private void OnControllerColliderHit(ControllerColliderHit hit)
|
||||
{
|
||||
Rigidbody body = hit.collider.attachedRigidbody;
|
||||
// 只推动带有刚体且非静态的物体
|
||||
if (body != null && !body.isKinematic)
|
||||
{
|
||||
// 忽略从下方碰撞
|
||||
if (hit.moveDirection.y < -0.3f) return;
|
||||
|
||||
Vector3 pushDir = new Vector3(hit.moveDirection.x, 0, hit.moveDirection.z);
|
||||
body.AddForce(pushDir * pushPower, ForceMode.Impulse);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetSpeed(float newSpeed)
|
||||
{
|
||||
speed = newSpeed;
|
||||
}
|
||||
|
||||
|
||||
public void ResetSpeed()
|
||||
{
|
||||
speed = initSpeed;
|
||||
|
||||
Reference in New Issue
Block a user