1
\$\begingroup\$

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.

\$\endgroup\$
-1
\$\begingroup\$

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.

|improve this answer|||||
\$\endgroup\$
  • 1
    \$\begingroup\$ You don't need an interface for OnCollisionEnter. Also having a separate class for each type of Wall seems like overkill. \$\endgroup\$ – bummzack Jul 22 '14 at 6:51
  • \$\begingroup\$ The interface is to make sure the implementation of OnCollisionEnter is provided. \$\endgroup\$ – S.C. Jul 22 '14 at 7:38
  • \$\begingroup\$ 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 \$\endgroup\$ – user49628 Jul 22 '14 at 8:21
  • \$\begingroup\$ 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. \$\endgroup\$ – S.C. Jul 22 '14 at 8:38
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – S.C. Jul 22 '14 at 9:09
2
\$\begingroup\$

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.

Tags menu

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.

|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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