using System.Data;
using UnityEngine;
public class TestMovementNmr2 : MonoBehaviour
{
public float walkSpeed = 10f;
public float currentSpeed;
public float shiftSpeed = 3f;
private CharacterController controller;
Vector3 velocity;
public float jumpHeight = 3f;
public float shiftJumpHeight = 1f;
public float groundCheckDistance = 0.4f;
public LayerMask groundMask;
public Transform groundCheck;
public float gravity = -18f;
bool isGrounded;
private void Start()
{
controller = GetComponent<CharacterController>();
currentSpeed = walkSpeed;
}
private void Update()
{
// Ground check
isGrounded = Physics.CheckSphere(groundCheck.position, groundCheckDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f; // Keeps the player stuck to the ground
}
// Movement input
float X = Input.GetAxis("Horizontal");
float Z = Input.GetAxis("Vertical");
Vector3 move = transform.right * X + transform.forward * Z;
// shift speed
if (Input.GetKey(KeyCode.LeftShift) && isGrounded)
currentSpeed = shiftSpeed;
else
currentSpeed = walkSpeed;
controller.Move(move * currentSpeed * Time.deltaTime);
// Jumping
if (Input.GetButtonDown("Jump") && isGrounded)
{
float jumpMultiplier = Input.GetKey(KeyCode.LeftShift) ? 0.5f : 1f;
velocity.y = Mathf.Sqrt(jumpHeight * jumpMultiplier * -2f * gravity);
}
// Gravity
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}