First of all, since I’m a newbie, I don’t even know if I’m asking this in the correct format. Basically I have a floor with a box collider, and on top of it a player (capsule) with the main camera paired with the player, the player has a rigidbody, the playermovement script and the capsule collider. I attach photos:
Además, adjunto el script:
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed = 5f;
public float mouseSensitivity = 100f;
public Transform playerBody;
private Rigidbody rb;
private float xRotation = 0f;
private bool isGrounded;
void Start()
{
rb = GetComponent<Rigidbody>();
if (rb == null)
{
Debug.LogError("No se encontró un Rigidbody en el jugador.");
}
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
isGrounded = IsGrounded();
if (isGrounded)
{
Debug.Log("El jugador está tocando el suelo.");
}
else
{
Debug.Log("El jugador no está tocando el suelo.");
}
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
playerBody.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
transform.Rotate(Vector3.up * mouseX);
}
void FixedUpdate()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector3 movement = transform.forward * vertical + transform.right * horizontal;
rb.MovePosition(rb.position + movement * speed * Time.fixedDeltaTime);
}
private bool IsGrounded()
{
RaycastHit hit;
if (Physics.Raycast(transform.position, Vector3.down, out hit, 1f))
{
Debug.Log("Colisión con: " + hit.collider.name);
return true;
}
return false;
}
}
I don’t think anyone will answer me, but I’ve had this problem for days and the only thing I have left to try is to ask here. Thank you very much in advance if anyone answers me.