- Home /
Falling through floor glitch
I'm making a simple 2.5 D platformer, and I'm running into a problem where the Player object is falling through the floor. It only happens sometimes; it only happens above a certain height, and not predictably even then. The player object has a rigidbody and a box collider, and some floors are planes, and some are cubes, each with their own simple colliders. I've fallen through both the cubes and the planes, and it seems to happen at random, and extreme heights don't seem to be a guarantee of recreating the glitch. I'm controlling the Player Character with a simple C# script (I plan to add more features after I get this part functioning reliably):
using UnityEngine; using System.Collections; public class PlayerController : MonoBehaviour {
public float moveSpeed, jumpForce;
Rigidbody rb;
float horizontal;
void Start () {
rb = GetComponent<Rigidbody> ();
}
void Update () {
if (Input.GetKeyDown (KeyCode.UpArrow) || Input.GetKeyDown ("w")) {
Jump ();
}
horizontal = Input.GetAxisRaw("Horizontal");
Vector3 movement = new Vector3 (horizontal * moveSpeed, rb.velocity.y, 0f);
rb.velocity = movement;
}
void Jump(){
rb.velocity = new Vector3 (rb.velocity.x, 0f, 0f);
rb.AddForce (Vector3.up * jumpForce);
}
}
Can anyone explain why this is happening so I can fix it?
Answer by Foulcloud · Aug 24, 2015 at 09:48 PM
Try pausing Unity mid jump and then advance frame by frame. If you find that the player can completely miss a collider from one frame to the next then this is the problem. Higher speeds and a very thin collider on the plane could be problematic.
If this is the case then you could make bigger colliders or use raycasts. With raycasting you could check the new "ground hit" position first and if it is greater than the raycast hit from the last position then use the hit position of the previous raycast as the new player position.
(Raycast)... or compare yVelocity*deltaTime and raycast length, if velocity greater than length, set velocity to raycast length (or just set position directly)