using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 5f;
public float sprintMultiplier = 1.5f;
public float lookSpeed = 3f;
public float jumpForce = 5f;
public float gravityMultiplier = 2f; // gravitácio szorzo
public Transform playerCamera;
private Rigidbody rb;
private float xRotation = 0f;
private bool isGrounded = true;
void Start()
{
rb = GetComponent<Rigidbody>();
Cursor.lockState = CursorLockMode.Locked;
rb.freezeRotation = true; // felfele ne lefele ez is nagyon fontos része
}
void Update()
{
Move();
Look();
Jump();
}
void FixedUpdate()
{
ApplyExtraGravity(); // feather falling enchantment ismételten ne felejtsd el pls
}
void Move()
{
float moveX = Input.GetAxis("Horizontal");
float moveZ = Input.GetAxis("Vertical");
float speed = moveSpeed;
if (Input.GetKey(KeyCode.LeftShift))
{
speed *= sprintMultiplier;
}
Vector3 move = transform.right * moveX + transform.forward * moveZ;
rb.MovePosition(rb.position + move * speed * Time.fixedDeltaTime);
}
void Look()
{
float mouseX = Input.GetAxis("Mouse X") * lookSpeed;
float mouseY = Input.GetAxis("Mouse Y") * lookSpeed;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
playerCamera.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
transform.Rotate(Vector3.up * mouseX);
}
void Jump()
{
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
rb.velocity = new Vector3(rb.velocity.x, 0, rb.velocity.z); // a vertikális hogymondják ne felejsd el jövöbeli énem
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
isGrounded = false;
}
}
void ApplyExtraGravity()
{
if (!isGrounded && rb.velocity.y < 0)
{
// stronger gravity when falling
rb.AddForce(Vector3.down * gravityMultiplier, ForceMode.Acceleration);
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.contacts[0].normal.y > 0.5f)
{
isGrounded = true;
}
}
}