diff options
Diffstat (limited to 'JamHelper/Assets/JamHelper/JamUtils/FirstPersonCharacterController/Rigidbody/MoveByVelocity/Scripts')
2 files changed, 53 insertions, 24 deletions
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 6f95732..66040f4 100644 --- a/JamHelper/Assets/JamHelper/JamUtils/FirstPersonCharacterController/Rigidbody/MoveByVelocity/Scripts/FPSCharacterController.cs +++ b/JamHelper/Assets/JamHelper/JamUtils/FirstPersonCharacterController/Rigidbody/MoveByVelocity/Scripts/FPSCharacterController.cs @@ -86,9 +86,11 @@ namespace JamUtils.FirstPersonCharacterController.RigidbodyVelocity [SerializeField] private Vector3 m_ExtraGravity; [Header("WalkOnStairs")] - [SerializeField] private Transform m_Lower; - [SerializeField] private Transform m_Upper; - [SerializeField] private float m_StepSmooth; + [SerializeField] private Transform m_Foot; + [SerializeField] private float m_StairMaxHeight = 0.35f; // 最大允许跨上的高度 + [SerializeField] private int m_StairClusterCheckCount = 10; // 最后一个是上限 + [SerializeField] private float m_StairCheckDistance = 2f; + private float m_StairCheckDeltaHeight { get { return m_StairMaxHeight / m_StairClusterCheckCount; } } #endregion @@ -159,6 +161,8 @@ namespace JamUtils.FirstPersonCharacterController.RigidbodyVelocity return; } + m_Rigidbody.useGravity = m_GroundChecker.isOnGround && !m_GroundChecker.isOnSlope; + float moveX = Input.GetAxisRaw("Horizontal"); float moveZ = Input.GetAxisRaw("Vertical"); @@ -167,7 +171,7 @@ namespace JamUtils.FirstPersonCharacterController.RigidbodyVelocity Vector3 dir = right * moveX + forward * moveZ; - if(dir.magnitude == 0) + if (dir.magnitude == 0) { m_MoveDirection = Vector3.zero; return; @@ -193,19 +197,11 @@ namespace JamUtils.FirstPersonCharacterController.RigidbodyVelocity 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; - } - } - Debug.Log(m_MoveDirection); - m_MoveDirection = dir; } @@ -217,7 +213,6 @@ namespace JamUtils.FirstPersonCharacterController.RigidbodyVelocity if (!m_GroundChecker.isOnGround) return; - //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; @@ -393,21 +388,51 @@ namespace JamUtils.FirstPersonCharacterController.RigidbodyVelocity m_Rigidbody.AddForce(m_ExtraGravity, ForceMode.Acceleration); } + /// <summary> + /// 上阶梯,通过一些列簇cluster检测 + /// </summary> void WalkOnStairs() { - //int layermask = ~(1 << LayerMask.NameToLayer("Player")); + if(m_GroundChecker.isOnSlope) + { + return; + } - //RaycastHit hitLower; - //if (Physics.Raycast(m_Lower.position, transform.TransformDirection(Vector3.forward), out hitLower, 0.6f, layermask)) - //{ - // RaycastHit hitUpper; - // if (!Physics.Raycast(m_Upper.position, transform.TransformDirection(Vector3.forward), out hitUpper, 0.7f, layermask)) - // { - // m_Rigidbody.position += new Vector3(0f, m_StepSmooth * Time.deltaTime, 0f); - // } - //} - } + if (m_MoveDirection.magnitude == 0) + { + return; + } + + int check = -1; + for(int i = 0; i < m_StairClusterCheckCount; ++i) + { + Vector3 origin = m_Foot.position + new Vector3(0, i*m_StairCheckDeltaHeight, 0); + + GizmosHandle.Instance.DoGizmos(() => + { + Gizmos.DrawLine(origin, origin + Vector3.forward * m_StairCheckDistance); + }); + + if (!Physics.Raycast(origin, m_MoveDirection, m_StairCheckDistance)) + { + break; + } + check = i; + } + + if (check != -1) + { + Debug.Log(check); + } + + if (check < m_StairClusterCheckCount - 1) // OnStair + { + Vector3 pos = m_Rigidbody.position; + pos.y = Mathf.Lerp(pos.y, (check + 1)* m_StairCheckDeltaHeight + pos.y, 0.25f); + m_Rigidbody.position = pos; + } + } void SetCamera () { 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 01538e1..240c61a 100644 --- a/JamHelper/Assets/JamHelper/JamUtils/FirstPersonCharacterController/Rigidbody/MoveByVelocity/Scripts/GroundChecker.cs +++ b/JamHelper/Assets/JamHelper/JamUtils/FirstPersonCharacterController/Rigidbody/MoveByVelocity/Scripts/GroundChecker.cs @@ -40,6 +40,10 @@ namespace JamUtils.FirstPersonCharacterController.RigidbodyVelocity { get { + if(!isOnGround) + { + return false; + } RaycastHit hitInfo; if (Physics.Raycast(foot.position, Vector3.down, out hitInfo)) { |