- Home /
2D collision not working
I'm trying to make a collectible that will destroy itself once collected. problem is unlike anything I've coded before the object that will collide with the collectible is not moving but spawned in ontop of the collectible. (it's a game similar to snake where the body of your snake consists of blocks and each block get's spawned in to move "forwards"). Right now the underneath is my code and it is not tracking the collision. I've tried onTriggerEnter2D, onCollisionEnter2D, onTriggerStay2D and onCollisionStay2D.
anyone have a clue as to why this could be fucking up? and no I don't have "is trigger" checked when I'm making it into a collision detection instead.
edit: I was told to add a rigidbody2D to my snakebody and make it kinematic but it still doesn't run the code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CollectiblesController : MonoBehaviour
{
public void OnCollisionStay2D(Collision2D coll)
{
if (coll.gameObject.tag == "SnakeBody")
{
Destroy(this.gameObject);
}
}
}
Answer by SandorChickane · Apr 07, 2020 at 12:03 PM
Does one of either of the two objects (the snake 'block' or the collectible) have a Rigidbody2D component? You need one of the objects to have a Rigidbody for collisions to register - if you don't want it to be affected by physics, you can just make it a Kinematic rigidbody and it will just be used to register collisions :)
I added the rigidbody2D to the snakebody and put it to kinematic. It still doesn't run the code :( tried all 4 again, collisionenter/stay2D and triggerenter/stay2D
am I supposed to have a box collider2D on both of them at the same time?
Never $$anonymous$$d got it to work! the collision box I had on my snake was a 3D box and not 2D! still thank you sandorchickane for telling me about the rigidbody cuz I didn't know that part! <3
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to add a score when the correct object drags to the correct box? 1 Answer
Image display from IP camera 0 Answers