# Player drops thru floor?

I’m just mucking around with 2D stuff - I have a player (a square), and a platform (a rectangle) with a box collider. All I want to happen is the player to fall and land on the platform. I’m doing a ray cast downwards, which works, except depending on what the player’s starting height is, it sometimes stops IN the platform instead of ON it.

It’s meant to do a raycast downwards, then if the collision distance is less than the falling speed, it adjusts the falling speed (vSpeed) to match, then sets the collision flag to True. This, logically, should put the player neatly on top of the platform. I’m not sure why this doesn’t work.

``````using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {
public float vSpeed;
float maxVSpeed;
public bool collided;

// Use this for initialization
void Start () {
vSpeed = 0;
maxVSpeed = 1.0f;
collided = false;
}

// Update is called once per frame
void Update () {
if (collided) {
return;
}
vSpeed += 0.01f;
if (vSpeed >= maxVSpeed) {
vSpeed = maxVSpeed;
}

RaycastHit2D hitInfo = Physics2D.Raycast (transform.position, new Vector2(0,-maxVSpeed));
if (hitInfo.distance <= vSpeed) {
vSpeed = hitInfo.distance;
collided = true;
}
transform.Translate (new Vector3 (0,0-vSpeed,0));
if (collided) {
vSpeed = 0;
}
}
}
``````

Generally speaking it’s easier to start your raycast above the player so it’ll still work properly if the player is sitting directly on the floor. For example, if you start your raycast 0.5 metres higher on the Y axis you should expect a value of 0.5 or less if they’re standing on the floor. At that point you can just force the player’s Y position to be (floor + 0.5)

Ah, seems to be working better now. Thanks.