Better controller (infinite speed building glitch though)

This commit is contained in:
Warwick 2025-08-01 16:28:20 +01:00
parent 18f0a6ef89
commit c20c527b72

View file

@ -6,14 +6,12 @@ public class PlayerControllerV2 : MonoBehaviour
private CharacterController m_CharacterController;
private Camera m_Camera;
private bool m_IsGrounded = false;
private bool m_WantJump = false;
public float m_LookSensitivity = 25f;
public float m_PlayerSpeed = 10f;
private Vector2 m_MoveInput = Vector2.zero;
private Vector2 m_LookInput = Vector2.zero;
private Vector3 m_Velocity = Vector3.zero;
private float m_MovementSmoothing = .05f;
private bool m_isColliding = false;
public float m_MaxSpeed = 5f;
void Awake()
{
@ -50,14 +48,36 @@ private void Move()
{
Vector3 direction = transform.right * m_MoveInput.x +
transform.forward * m_MoveInput.y;
m_CharacterController.Move(direction * m_PlayerSpeed * Time.deltaTime);
m_Velocity += -transform.up * 9.81f * Time.deltaTime; // Gravity
// Reset negative velocity when landed
if (m_IsGrounded && m_Velocity.y < 0)
{
m_Velocity.y = -1 * Time.deltaTime;
}
// Apply input to velocity only when on the ground
if (m_IsGrounded)
{
m_Velocity += direction * m_PlayerSpeed * Time.deltaTime;
}
// Apply friction when on the ground and not moving
if (m_IsGrounded && m_MoveInput.magnitude < .1f /*dead zone*/)
{
m_Velocity.x /= 1.1f;
m_Velocity.z /= 1.1f;
}
m_CharacterController.Move(m_Velocity * Time.deltaTime);
}
void OnJump()
{
if (m_IsGrounded)
{
//m_CharacterController.AddForce(new Vector3(0f,400f,0f));
m_Velocity.y = 10;
m_IsGrounded = false;
}
}