diff options
5 files changed, 89 insertions, 36 deletions
diff --git a/JamHelper/Assets/JamHelper/JamUtils/FirstPersonCharacterController/Rigidbody/MoveByVelocity/Scenes/FPSCharacterController.unity b/JamHelper/Assets/JamHelper/JamUtils/FirstPersonCharacterController/Rigidbody/MoveByVelocity/Scenes/FPSCharacterController.unity index 5b55a41..d112fce 100644 --- a/JamHelper/Assets/JamHelper/JamUtils/FirstPersonCharacterController/Rigidbody/MoveByVelocity/Scenes/FPSCharacterController.unity +++ b/JamHelper/Assets/JamHelper/JamUtils/FirstPersonCharacterController/Rigidbody/MoveByVelocity/Scenes/FPSCharacterController.unity @@ -9161,6 +9161,10 @@ PrefabInstance: propertyPath: m_LocalEulerAnglesHint.z value: 0 objectReference: {fileID: 0} + - target: {fileID: 7739210602112578985, guid: 8ec8551d12bf0c649a5ba9d04f879de5, type: 3} + propertyPath: m_UseGravity + value: 1 + objectReference: {fileID: 0} - target: {fileID: 7739210602112578987, guid: 8ec8551d12bf0c649a5ba9d04f879de5, type: 3} propertyPath: m_Name value: Player diff --git a/JamHelper/Assets/JamHelper/JamUtils/FirstPersonCharacterController/Rigidbody/MoveByVelocity/Scripts/FPSCharacterController.cs b/JamHelper/Assets/JamHelper/JamUtils/FirstPersonCharacterController/Rigidbody/MoveByVelocity/Scripts/FPSCharacterController.cs index 753fa3b..6f95732 100644 --- a/JamHelper/Assets/JamHelper/JamUtils/FirstPersonCharacterController/Rigidbody/MoveByVelocity/Scripts/FPSCharacterController.cs +++ b/JamHelper/Assets/JamHelper/JamUtils/FirstPersonCharacterController/Rigidbody/MoveByVelocity/Scripts/FPSCharacterController.cs @@ -6,8 +6,10 @@ using UnityEngine; namespace JamUtils.FirstPersonCharacterController.RigidbodyVelocity { - // 第一人称角色控制 - public class FPSCharacterController : MonoBehaviour + /// <summary> + /// 第一人称角色控制 + /// </summary> + public class FPSCharacterController : MonoBehaviour { [Flags] public enum CharacterModule @@ -152,9 +154,12 @@ namespace JamUtils.FirstPersonCharacterController.RigidbodyVelocity return; if (!m_GroundChecker.isOnGround) - return; + { + m_Rigidbody.useGravity = true; + return; + } - float moveX = Input.GetAxisRaw("Horizontal"); + float moveX = Input.GetAxisRaw("Horizontal"); float moveZ = Input.GetAxisRaw("Vertical"); Vector3 right = transform.right; @@ -162,26 +167,46 @@ namespace JamUtils.FirstPersonCharacterController.RigidbodyVelocity Vector3 dir = right * moveX + forward * moveZ; - if (IsModuleActive(CharacterModule.WalkOnSlope)) + if(dir.magnitude == 0) + { + m_MoveDirection = Vector3.zero; + return; + } + + dir = Vector3.Slerp(m_MoveDirection, dir, 1f).normalized; + + if (IsModuleActive(CharacterModule.WalkOnSlope)) { + // 处理斜面,要把方向投射在斜面上 if (m_GroundChecker.isOnGround) { RaycastHit hitInfo; if (Physics.Raycast(m_GroundChecker.foot.position, Vector3.down, out hitInfo)) { Vector3 normal = hitInfo.normal; - dir = Vector3.ProjectOnPlane(dir, normal); - GizmosHandle.Instance.DoGizmos(() => - { - Gizmos.DrawLine(hitInfo.point + new Vector3(0, 0.1f, 0), hitInfo.point + dir); - }); - } + if(normal != Vector3.up) // on slope + { + dir = Vector3.ProjectOnPlane(dir, normal); + dir = dir.normalized; + GizmosHandle.Instance.DoGizmos(() => + { + Gizmos.DrawLine(hitInfo.point + new Vector3(0, 0.1f, 0), hitInfo.point + dir * 3f); + }); + m_Rigidbody.useGravity = false; + Debug.Log("On Slope"); + } + } } - } + else + { + m_Rigidbody.useGravity = true; + } - dir = dir.normalized; + } + + Debug.Log(m_MoveDirection); - m_MoveDirection = Vector3.Slerp(m_MoveDirection, dir, 1f); + m_MoveDirection = dir; } void MoveAroundFixedUpdate() @@ -192,8 +217,8 @@ namespace JamUtils.FirstPersonCharacterController.RigidbodyVelocity if (!m_GroundChecker.isOnGround) return; - float vy = m_Rigidbody.velocity.y; - Vector3 velocity = new Vector3(m_MoveDirection.x * Time.deltaTime * m_MoveSpeed, vy, m_MoveDirection.z * Time.deltaTime * m_MoveSpeed); + //Vector3 velocity = new Vector3(.x * Time.deltaTime * m_MoveSpeed, , m_MoveDirection.z * Time.deltaTime * m_MoveSpeed); + Vector3 velocity = m_MoveDirection * Time.fixedDeltaTime * m_MoveSpeed; Vector3 rigidVel = m_Rigidbody.velocity; @@ -277,7 +302,6 @@ namespace JamUtils.FirstPersonCharacterController.RigidbodyVelocity void DodgeFixed() { - } void Shot() diff --git a/JamHelper/Assets/JamHelper/JamUtils/FirstPersonCharacterController/Rigidbody/MoveByVelocity/Scripts/GroundChecker.cs b/JamHelper/Assets/JamHelper/JamUtils/FirstPersonCharacterController/Rigidbody/MoveByVelocity/Scripts/GroundChecker.cs index 2b2d282..01538e1 100644 --- a/JamHelper/Assets/JamHelper/JamUtils/FirstPersonCharacterController/Rigidbody/MoveByVelocity/Scripts/GroundChecker.cs +++ b/JamHelper/Assets/JamHelper/JamUtils/FirstPersonCharacterController/Rigidbody/MoveByVelocity/Scripts/GroundChecker.cs @@ -5,27 +5,49 @@ using UnityEngine; namespace JamUtils.FirstPersonCharacterController.RigidbodyVelocity { - public class GroundChecker : MonoBehaviour - { - [SerializeField] private Transform m_Foot; + /// <summary> + /// 使用碰撞体进行地面检测 + /// </summary> + [RequireComponent(typeof(Collider))] + public class GroundChecker : MonoBehaviour + { + [SerializeField] private Transform m_Foot; - private List<Collider> m_Colliders = new List<Collider>(); + private List<Collider> m_Colliders = new List<Collider>(); - public Transform foot - { - get - { - return m_Foot; - } - } + public Transform foot + { + get + { + return m_Foot; + } + } - public bool isOnGround - { - get - { - return m_Colliders.Count != 0; - } - } + // 下次物理模拟之前保证有效 + public bool isOnGround + { + get + { + return m_Colliders.Count != 0; + } + } + + + /// <summary> + /// 是否在斜坡之上 + /// </summary> + public bool isOnSlope + { + get + { + RaycastHit hitInfo; + if (Physics.Raycast(foot.position, Vector3.down, out hitInfo)) + { + return hitInfo.normal != Vector3.up; + } + return false; + } + } private void OnTriggerEnter(Collider other) { diff --git a/JamHelper/Assets/JamHelper/JamUtils/Physics/PhysicsHelper.cs b/JamHelper/Assets/JamHelper/JamUtils/Physics/PhysicsHelper.cs index 7cd1e5a..ad033b6 100644 --- a/JamHelper/Assets/JamHelper/JamUtils/Physics/PhysicsHelper.cs +++ b/JamHelper/Assets/JamHelper/JamUtils/Physics/PhysicsHelper.cs @@ -7,7 +7,7 @@ using UnityEngine; // 参考 // * https://www.youtube.com/watch?v=BNiAt0HnC5M&ab_channel=DitzelGames -namespace JamUtils.Physics +namespace JamUtils.PhysicsUtils { public static class PhysicsHelper diff --git a/JamHelper/Assets/JamHelper/JamUtils/Scripts/Smooth-Motion-in-Unity/InterpolationFactorController.cs b/JamHelper/Assets/JamHelper/JamUtils/Scripts/Smooth-Motion-in-Unity/InterpolationFactorController.cs index 17bc434..ba03d98 100644 --- a/JamHelper/Assets/JamHelper/JamUtils/Scripts/Smooth-Motion-in-Unity/InterpolationFactorController.cs +++ b/JamHelper/Assets/JamHelper/JamUtils/Scripts/Smooth-Motion-in-Unity/InterpolationFactorController.cs @@ -1,5 +1,8 @@ using UnityEngine; +/// <summary> +/// 处理抖动jitter问题 +/// </summary> [DisallowMultipleComponent] [DefaultExecutionOrder(ORDER_EXECUTION)] public class InterpolationFactorController : MonoBehaviour |