Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am doing a very simple scene with a cube sliding on the "ground". Once the cube reach a wall, he is blocked.

I want to detect the wall to make the cube go in the other direction. I used OnCollisionEnter but it is triggered at the scene start as my cube touch the ground.

I could make a difference between grounds and wall, but my game will make the whole scene rotate, and floor will become wall and wall floor.

I wanted to add "physics whatever object" to allow a left/right detection but i cannot add collider or rigidbody to my cube.

How can i have a cube knowing when he touch a wall without having a false positive with the ground ?

share|improve this question
What kind of components do you have attached to your cube currently and what functions are you calling to move it? Is it just a cube with a BoxCollider? – chaosTechnician Sep 23 '11 at 2:28

I could make a difference between grounds and wall, but my game will make the whole scene rotate, and floor will become wall and wall floor.

Why couldn't you just tag the two sets of walls (horizontal and vertical), and then do tag check in OnCollisionEnter for horizontal or vertical as a wall check compared to the rotation of the world? So, for example, if the world is rotated by 90 degrees, you "hit a wall" when you collide with something that was marked as horizontal.

share|improve this answer

Look at the contact normals, ContactPoint.normal. These are vectors perpendicular to the contact surfaces, so if you hit a wall the normal will be mostly horizontal and if you hit the floor the normal will be mostly vertical (unless the geometry of your walls and floors is especially gnarly).

A collision may have multiple contact points (Collision.contacts); averaging/summing their normals would probably work well.

You might also try comparing the average of the points of contact (ContactPoint.point) with the center position of your cube. That might well be more robust.

(I haven't tried actually doing this in Unity.)

share|improve this answer

Instead of using 'just' a cube, you could use a prefab with a cube, and add some empty gameObjects with triggers or colliders to check collsions on different positions.

share|improve this answer

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.