- Home /
Question by
costin88boss · Apr 28, 2020 at 05:14 AM ·
2dgameobjectenemyno errors
can't detect what player collides with, tried everything and all other posts...
so i want to make a 2d game where when player collides with spike it triggers something (for now i want debug.log to trigger just to be sure)
void OnCollisionEnter2D(Collision2D trig)
{
if (trig.gameObject.tag == "Enemy")
{
Debug.Log("player touches enemy");
}
well if it shows the code bad sorry. so, i am trying to detect the tag of the Enemy (spike) and make a trigger.
after that i will put a die script.
Comment
Best Answer
Answer by costin88boss · Apr 28, 2020 at 01:46 PM
edit:
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Enemy")
{
Debug.Log("OH SHIT");
}
from the Unity Manual works..
Answer by Luis_Gan · Apr 28, 2020 at 05:28 AM
The spike must have a non kinematic rigidbody attached to.
Or instead of attaching the script to the player, you attach it to the spike.
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameobject.CompareTag("Player"))
{
Debug.Log("I am the enemy and the player touched me");
}