It's likely you're always going to get a collision with the floor; if you're using gravity, your cube is essentially going to be almost constantly hitting the floor in addition to any other objects it hits. The trick is in determining if there are additional collisions that involve a wall.
Without knowing much about your specific implementation, here's what I'd suggest: In OnCollisionEnter
, check every ContactPoint
's normal to see if it's perpendicular to the floor. For now, I'll assume that your cube rotates to have its Forward
vector parallel to the ground on which it's moving (resulting in its Up
vector pointing away from the ground). If that's wrong, let me know how you have it set up and I'll try to edit the answer to fit. My code is in C#, if you're using JavaScript, there will obviously be some differences. In your cube's collision code, you'll want something like this:
void OnCollisionEnter(Collision other) {
bool hitWall = false;
foreach(ContactPoint cp in other.contacts) {
float dot = Vector3.Dot(cp.normal, transform.up);
if(dot < 1.0f && dot > -1.0f) {
hitWall = true;
break; //No need to keep checking once you've found a wall
}
}
if(hitWall) {
//At least one collision was not with the floor (or ceiling)
//Handle wall collisions here
}
}
If you still get false positives, consider using a numbers closer to zero than 1.0 and -1.0 (like 0.9 and -0.9) for a little less precision in determining a collision with a non-wall.