Move object on specific axis when hitting a trigger

I’m very new to programming, having done very, very little Java programming.

Will try to make a small drawing of what I need help with below:

 __________
|          |
|          |
|         x|
|__________|

When the X(in my case a cube) hits a wall, I want it to go to the opposite side. I want this to happen if it hits the upper wall as well.

I’m guessing I need to use onTrigger or something similar, but I don’t know how to define that one trigger is supposed to reverse the position on the X-axis, and the other on the Z-axis.

Little shaky on this, but I believe the below will work if you wanted UnityScript. Just add this script as a component to your wall GameObject and define wallX and wallZ as the inside edge of your walls. If your walls are actually four different GameObjects instead of one you could just chop out the x bits for the top/bottom walls and the z bits for the side walls.

You could get fancier with this, but this has the basic functionality you’re looking for. If it doesn’t work tell me the error, because I don’t have the chance to test it at the moment.

var wallX : int;
var wallZ : int;

function OnCollisionEnter(collision : Collision) {
    var collisionPoint : Vector3 = collision.contacts[0].point;
    var objectHit : Transform = GameObject.Find(collision.gameObject.name).transform;
    if(collisionPoint.x >= wallX ||collisionPoint.x <= -wallX) {
        if(collisionPoint.x > 0) objectHit.x -= 0.01;
        else objectHit.x += 0.01;
        objectHit.x *= -1;
    }
    if(collisionPoint.z >= wallZ ||collisionPoint.z <= -wallZ) {
        if(collisionPoint.z > 0) objectHit.z -= 0.01;
        else objectHit.z += 0.01;
        objectHit.z *= -1;
    }
}

hey … According to me just change the moving direction by 180 degree . By this your cube always moves in opposite direction when it hit something.