I have started a new Unity project and created a player with PlayerController script.
When I hit play, my player starts falling through the floor and after a few seconds it teleports up, to where it started falling. The floor, has a box collider and my player has a capsule collider and a rigidbody (both colliders have the isTrigger boolean set to false and they are both in the Default layer). I am using the same controller script I always use in my projects (unless I made a mistake and can’t find it).
Here’s that script:
public class PlayerController : MonoBehaviour
{
public Rigidbody playerRigidbody;
public Camera playerCamera;
public float speed = 3f;
public float mouseSensitivity = 5f;
public Vector3 cameraOffset = Vector3.zero;
private Vector3 playerPosition = Vector3.zero;
private Vector3 playerRotation = Vector3.zero;
private Vector3 cameraRotation = Vector3.zero;
void Start()
{
playerRigidbody = GetComponent<Rigidbody>();
playerPosition = playerRigidbody.position;
playerRotation = playerRigidbody.rotation.eulerAngles;
cameraRotation = playerCamera.transform.localEulerAngles;
}
void Update()
{
CalculateMovement();
playerCamera.transform.position = playerRigidbody.position + cameraOffset;
}
void FixedUpdate()
{
playerRigidbody.MovePosition(playerPosition);
playerRigidbody.MoveRotation(Quaternion.Euler(playerRotation));
playerCamera.transform.localEulerAngles = cameraRotation;
}
void CalculateMovement()
{
Vector3 xVel = transform.right * Input.GetAxis("Horizontal");
Vector3 zVel = transform.forward * Input.GetAxis("Vertical");
playerPosition += (xVel + zVel) * speed * Time.fixedDeltaTime;
Debug.Log((xVel + zVel) * speed * Time.fixedDeltaTime);
float xRot = -Input.GetAxis("Mouse Y");
float yRot = Input.GetAxis("Mouse X");
playerRotation += new Vector3(0f, yRot, 0f) * mouseSensitivity;
cameraRotation += new Vector3(xRot, yRot, 0f) * mouseSensitivity;
}
}
As you can see I’ve included a Debug.Log()
, but it always prints 0.0 on the y axis.
EDIT:
I have removed the PlayerCotroller and the falling is not happening anymore, so there’s something wrong with it.
I would be very happy if you’d help me find the error.