# Simple square hit the wall and turn around without stopping

I am coding and making my very first plat former game in Unity 4.5 2D.

Basically I want my character to ping pong between two walls.

I have set up a GroundCheck and CeilingCheck. I was thinking to add a WallCheck on the left and right of the character to detect the walls, but I will have platforms in the air, and if the character doesn't make the jump and hits the platform on the side I don't want it to turn around, but simply slide off and continue in it's current direction until it hits the wall again.

Coding in C# 1. Get the character ping pong from wall to wall 2. Know the difference between hitting the wall and hitting the side of a platform.

Define an interface IWall.

using UnityEngine;
using System.Collections;

public interface IWall {
void OnCollisionEnter(Collision collision);
}


It is nice to separate the vertical and horizontal walls in to two classes as their logic for handling collisions is different.

VerticalWall

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(BoxCollider))]
public class VerticalWall : MonoBehaviour, IWall {
public void OnCollisionEnter(Collision collision) {
if (collision.rigidbody) {
// You can add forces to the collided object, change its velocity etc.
Vector3 v = collision.rigidbody.velocity;
v.x *= -1;
collision.rigidbody.velocity = v;
}
}
}


HorizontalWall

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(BoxCollider))]
public class HorizontalWall : MonoBehaviour, IWall {
public void OnCollisionEnter(Collision collision) {
if (collision.rigidbody) {
// You can add forces to the collided object, change its velocity etc.
Vector3 v = collision.rigidbody.velocity;
v.y *= -1;
collision.rigidbody.velocity = v;
}
}
}


Remember to attach a Rigidbody component to your character GameObject.

If you want to have your character to slide down along the vertical wall, you can set its velocity as v.x = 0 rather than v.x *= -1.

• You don't need an interface for OnCollisionEnter. Also having a separate class for each type of Wall seems like overkill. Commented Jul 22, 2014 at 6:51
• The interface is to make sure the implementation of OnCollisionEnter is provided.
– S.C.
Commented Jul 22, 2014 at 7:38
• I have been trying lot of different things but I keep getting this error. Assets/Sample Assets/2D/Scripts/VerticalWall.cs(5,14): error CS0737: VerticalWall' does not implement interface member IWall.OnCollisionEnter(UnityEngine.Collision)' and the best implementing candidate VerticalWall.OnCollisionEnter(UnityEngine.Collision)' in not public Commented Jul 22, 2014 at 8:21
• Apologize for my mistake. The code snippet is now updated. @bummzack is right that the interface is not a must. I use it as a reminder for you to implement the function OnCollisionEnter solely. In C#, interfaces can only have public methods.
– S.C.
Commented Jul 22, 2014 at 8:38
• By the way, @Blue made use of the tag system which is a great functionality provided by Unity. You may give it a shot as well.
– S.C.
Commented Jul 22, 2014 at 9:09

If your platforms are isolated use Tags.

Each object and prefab in the Unity game world can be interacted with differently depending on the tag they have.

For a wall of any kind simply tag it as "Wall" and code it with this is mind when making collision detection on your main character.

For a platform, guess what, tag it as "Platform". You can then implement a different collision behaviour for each object type using OnCollisionEnter:

public class ExampleClass : MonoBehaviour {
void OnCollisionEnter(Collision collision) {
foreach (ContactPoint contact in collision.contacts) {
switch(contact.tag){
case "Wall":
CollideWithWall(contact);
break;

case "Platform":
ClimbUpPlatform(contact);
break;

// Some fun examples of how to expand:
case "BouncyFloor":
break;

case "QuickSand":
break;
}
}
}
}


From there - create your methods: CollideWithWall and ClimbUpPlatform` to handle what affects your character.