Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How can i make a player stop when he hits the obsticle

i've watched the 5th episode of brackey's tutorials "how to make a video game in unity COLLISION " But still i can't figure it out. it doesn't even show the message in the console when you hit an obsticle or the ground. please help me maybe you have a code for that

Answers

  • Before you run the code, You must add a layer called Obstacle in your GameObject.


    using System.Collections;

    using System.Collections.Generic;

    using UnityEngine;


    public class CollisionDetection : MonoBehaviour

    {

        void OnCollisionEnter2D(Collider2D collision)

        {

            //Detect if this collision hit the other collision with a name of the layer.

            if (collision.gameObject.layer == LayerMask.NameToLayer("Obstacle"))

            {

    //Debug the collision.

                Debug.Log(collision + "is touched!");

            }

        }

    }

Sign In or Register to comment.