-1

I'm making a game in Unity where I want collisions between two moving objects to be detected (One of which is moved by the player using touch. For testing reasons I'm currently writing the script for mouse controls). However for some reason when the game object that is moving moves into the collider field of the object that needs to trigger an event when collided with, nothing happens. I added colliders to both objects and added Is trigger to the collider of the object that needs to trigger the event and as required but it still doesn't work.I tried it with the code that is supposed to trigger the event first and it didn't work and then I simply tried to use debug.log to see if the problem was with the code related to the event I want to get triggered but nothing works. Does anyone know how I might solve this problem?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Collision : MonoBehaviour
{
    private void OnTriggerEnter2D(Collider2D other)
    {
        Debug.Log("Hit Detected");
    }
}
derHugo
  • 83,094
  • 9
  • 75
  • 115
Maurice Bekambo
  • 325
  • 6
  • 21
  • are both colliders `XCollider2D`? Otherwise you should probably be using `OnTriggerEnter(Collider other)` instead ([Colliders manual](https://docs.unity3d.com/Manual/CollidersOverview.html)) – derHugo Feb 20 '19 at 17:03
  • @derHugo Both colliders are Circle Collider 2D's – Maurice Bekambo Feb 20 '19 at 17:11
  • Do you have a `RigidBody` attached to both objects? – Ben Rubin Feb 20 '19 at 17:25
  • No they don't because I don't actually want my objects to be acted upon by gravity or drag. I just want them to be able to move – Maurice Bekambo Feb 20 '19 at 17:29
  • 1
    You can set your `RigidBody` so that gravity doesn't affect them, but you'll need one for your colliders to work. From the Unity docs: `Notes: Collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached.` – Ben Rubin Feb 20 '19 at 17:57
  • Actually now it works thanks man @Ben Rubin – Maurice Bekambo Feb 20 '19 at 18:48

1 Answers1

2

You need to put a Rigibody on the 2 objects, if your project is:

  • 2D: "Gravity Scale" to 0 (so they don't have gravity)
  • 3D: Set checkBox "Use Gravity" to false
Ferran
  • 55
  • 1
  • 9