|
|
@ -19,47 +19,57 @@ using UnityEngine; |
|
|
|
|
|
|
|
|
|
|
|
public class PlayerMovement : MonoBehaviour |
|
|
|
public class PlayerMovement : MonoBehaviour |
|
|
|
{ |
|
|
|
{ |
|
|
|
[Header("Character")] |
|
|
|
[Header("Character")] |
|
|
|
[SerializeField] private CharacterController controller; |
|
|
|
[SerializeField] private CharacterController controller; |
|
|
|
|
|
|
|
|
|
|
|
[Header("Attributes")] |
|
|
|
[Header("Attributes")] |
|
|
|
[SerializeField] private FloatReference playerSpeed; |
|
|
|
[SerializeField] private FloatReference playerWalkSpeed; |
|
|
|
[SerializeField] private FloatReference jumpHeight; |
|
|
|
[SerializeField] private FloatReference playerRunSpeed; |
|
|
|
|
|
|
|
[SerializeField] private FloatReference jumpHeight; |
|
|
|
|
|
|
|
|
|
|
|
[Header("Environment")] |
|
|
|
[Header("Environment")] |
|
|
|
[SerializeField] private FloatReference gravity; |
|
|
|
[SerializeField] private FloatReference gravity; |
|
|
|
private Vector3 velocity; |
|
|
|
private Vector3 velocity; |
|
|
|
|
|
|
|
|
|
|
|
[Header("Ground")] |
|
|
|
[Header("Ground")] |
|
|
|
[SerializeField] private Transform groundCheck; |
|
|
|
[SerializeField] private Transform groundCheck; |
|
|
|
[SerializeField] private float groundDistance = 0.4f; |
|
|
|
[SerializeField] private float groundDistance = 0.4f; |
|
|
|
[SerializeField] private LayerMask groundMask; |
|
|
|
[SerializeField] private LayerMask groundMask; |
|
|
|
private bool isGrounded; |
|
|
|
private bool isGrounded; |
|
|
|
|
|
|
|
|
|
|
|
void Update() |
|
|
|
void Update() |
|
|
|
{ |
|
|
|
{ |
|
|
|
// Check if the player is on the ground and reset gravity |
|
|
|
// Check if the player is on the ground and reset gravity |
|
|
|
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask); |
|
|
|
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask); |
|
|
|
if (isGrounded && velocity.y < 0) |
|
|
|
if (isGrounded && velocity.y < 0) |
|
|
|
{ |
|
|
|
{ |
|
|
|
velocity.y = -2f; |
|
|
|
velocity.y = -2f; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Implement movement |
|
|
|
// Implement movement |
|
|
|
float x = Input.GetAxis("Horizontal"); |
|
|
|
float x = Input.GetAxis("Horizontal"); |
|
|
|
float z = Input.GetAxis("Vertical"); |
|
|
|
float z = Input.GetAxis("Vertical"); |
|
|
|
|
|
|
|
|
|
|
|
Vector3 move = transform.right * x + transform.forward * z; |
|
|
|
Vector3 move = transform.right * x + transform.forward * z; |
|
|
|
controller.Move(move * playerSpeed.Value * Time.deltaTime); |
|
|
|
if (Input.GetKeyDown(KeyCode.LeftShift) && isGrounded) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Debug.Log("Running"); |
|
|
|
|
|
|
|
controller.Move(move * playerRunSpeed * Time.deltaTime); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Debug.Log("Walking"); |
|
|
|
|
|
|
|
controller.Move(move * playerWalkSpeed.Value * Time.deltaTime); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Implement jumping |
|
|
|
// Implement jumping |
|
|
|
if(Input.GetButtonDown("Jump") && isGrounded) |
|
|
|
if (Input.GetButtonDown("Jump") && isGrounded) |
|
|
|
{ |
|
|
|
{ |
|
|
|
velocity.y = Mathf.Sqrt(jumpHeight.Value * -2f * gravity.Value); |
|
|
|
velocity.y = Mathf.Sqrt(jumpHeight.Value * -2f * gravity.Value); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Implement gravity |
|
|
|
// Implement gravity |
|
|
|
velocity.y += gravity.Value * Time.deltaTime; |
|
|
|
velocity.y += gravity.Value * Time.deltaTime; |
|
|
|
controller.Move(velocity * Time.deltaTime); |
|
|
|
controller.Move(velocity * Time.deltaTime); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|