using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public class PlayerMovement : MonoBehaviour.
{
Rigidbody rb;
CharacterController controller;
public float speed;
public float gravity;
public float jumpHeight;
public float minJump;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
public float originalHeight;
public float crouchHeight;
Vector3 velocity;
bool isGrounded;
private void Awake()
{
controller = GetComponent<CharacterController>();
}
private void Start()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = true;
}
void Update()
{
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(speed * Time.deltaTime * move);
#region Crouch.
if (Input.GetKeyDown(KeyCode.LeftControl))
{
controller.height = crouchHeight;
}
if(Input.GetKeyUp(KeyCode.LeftControl))
controller.height = originalHeight;
#endregion
#region Gravity.
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if(isGrounded && velocity.y < 0)
{
velocity.y = 0f;
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
#endregion
#region Jump.
//Z�plama
if (Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -5f * gravity);
}
#endregion
}
}