Tag Archives: collider

Setting up a fall detector in a Unity 2D game

This tutorial explains how to set up a fall detector in a Unity 2D game that detects when the player has fallen off a platform or off the map and respawns the player back to a checkpoint. This will be covered across three tutorials. We will look at how to add checkpoints in the next tutorial.

Sample code

Here is the sample C# code for the PlayerController script up to the point shown in the above video. This script will be completed in the next two tutorials.

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

  public float speed = 5f;
  public float jumpSpeed = 8f;
  private float movement = 0f;
  private Rigidbody2D rigidBody;
  public Transform groundCheckPoint;
  public float groundCheckRadius;
  public LayerMask groundLayer;
  private bool isTouchingGround;
  private Animator playerAnimation;

  // Use this for initialization
  void Start () {
    rigidBody = GetComponent<Rigidbody2D> ();
    playerAnimation = GetComponent<Animator> ();
  }
  
  // Update is called once per frame
  void Update () {
    isTouchingGround = Physics2D.OverlapCircle (groundCheckPoint.position, groundCheckRadius, groundLayer);
    movement = Input.GetAxis ("Horizontal");
    if (movement > 0f) {
      rigidBody.velocity = new Vector2 (movement * speed, rigidBody.velocity.y);
      transform.localScale = new Vector2(0.1483552f,0.1483552f);
    }
    else if (movement < 0f) {
      rigidBody.velocity = new Vector2 (movement * speed, rigidBody.velocity.y);
      transform.localScale = new Vector2(-0.1483552f,0.1483552f);
    } 
    else {
      rigidBody.velocity = new Vector2 (0,rigidBody.velocity.y);
    }

    if(Input.GetButtonDown ("Jump") && isTouchingGround){
      rigidBody.velocity = new Vector2(rigidBody.velocity.x,jumpSpeed);
    }

    playerAnimation.SetFloat ("Speed", Mathf.Abs (rigidBody.velocity.x));
    playerAnimation.SetBool ("OnGround", isTouchingGround);
  }

  void OnTriggerEnter2D(Collider2D other){
    if (other.tag == "FallDetector") {
      // what will happen when the player enters the fall detector code (this will be added later)
    }
    if (other.tag == "Checkpoint") {
      respawnPoint = other.transform.position;
    }
  }

}

Next tutorial: Adding checkpoints in the game

Physics2D materials and more 2D colliders in Unity

This video explains how to use Physics2D materials in Unity to add properties such as bounciness or friction to objects in your 2D game scene. We will also look at more 2D colliders in Unity including the box collider and polygon collider.

Next tutorial: More Physics2D materials

Adding physics and colliders to your 2D game in Unity

This video explains how to add physics and colliders to objects in a 2D game using Unity. In this tutorial, we will look at how to add the RigidBody2D component to an object in the game scene, as well as edge and circle colliders.

Next tutorial: Physics2D Materials and more 2D colliders

Rigid Bodies and Collision in PlayCanvas

This tutorial explains how to add the Rigid Body and Collision components to objects in your game so that your objects can move around and interact in a realistic way and so that you can detect when different objects collide in your game.

Rigid bodies allow the objects in your game to act under the control of physics. This means that your objects will be able to receive forces and move around in a realistic way. In order for an object to be able to interact with other objects and be under the influence of gravity or different forces (that can be controlled by scripts), it must have a Rigid Body component. You can change the properties of a rigid body such as friction, restitution (bounciness), mass, and so on.

The Collision component in PlayCanvas will give an object its physical form and change the way it behaves. There are several types of colliders (collision types) such as box, sphere, capsule, etc. If you give a box object a box collider, it will behave like a box. This means it will move around, slide, bounce, fall, and collide with other objects the way that a box would. If you give a sphere or ball object a sphere collider, then it will be able to bounce and roll around just like a real sphere or ball would. The Collision component is necessary for collision detection and triggers (something we will look at in another tutorial).

Watch the video below and then scroll down to see the sample code used for basic collision detection in the video.

Sample code for the collider.js script:

pc.script.create('collider', function (app) {
    // Creates a new Collider instance
    var Collider = function (entity) {
        this.entity = entity;
    };

    Collider.prototype = {
        // Called once after all resources are loaded and before the first update
        initialize: function () {
            this.entity.collision.on('collisionstart', this.onCollisionStart, this);
        },

        onCollisionStart: function(result){
            if(result.other.rigidbody){
                this.entity.destroy();
            }
        }
        
        
    };

    return Collider;
});