using UnityEngine;
public class Player : MonoBehaviour
{
[SerializeField] private float moveSpeed = 1f;
[SerializeField] private float jumpForce = 4.3f;
private Rigidbody rb;
private bool isGrounded;
private InputSystem_Actions input;
private Vector3 movement;
private void Awake()
{
input = new InputSystem_Actions();
rb = GetComponent<Rigidbody>();
}
private void OnEnable()
{
input.Enable();
}
private void Update()
{
PlayerInput();
if (input.Player.Jump.triggered && isGrounded)
{
Jump();
}
}
private void FixedUpdate()
{
// Обновляем только горизонтальные компоненты скорости, оставляя вертикальную (y) без изменений.
Vector3 newVelocity = new Vector3(movement.x * moveSpeed, rb.velocity.y, movement.z * moveSpeed);
rb.velocity = newVelocity;
}
private void PlayerInput()
{
Vector2 inputVector = input.Player.Move.ReadValue<Vector2>();
movement = new Vector3(inputVector.x, 0, inputVector.y);
}
private void Jump()
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
isGrounded = false;
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
isGrounded = true;
}
}
}