- Home /
OnTriggerEnter2D working, OnColliderEnter2D not working
I'm trying to call OnColliderEnter2D and log something, but it doesn't work. It works when I check "Is Trigger" on one of the colliders and call OnTriggerEnter2D, and that's enough for my needs, but I'd still like to know why OnColliderEnter doesn't work...
Based on what I've read, here's the relevant information:
The two objects are a "Player" and an "Obstacle" object
Both objects have a Collider2D component
The player object has a RigidBody2D component, Body Type set to Kinematic.
Code for obstacle object:
public class Obstacle : MonoBehaviour
{
Vector3 position;
float endYPosition;
float yVelocity;
Vector3 spawnPosition;
// Use this for initialization
void Start ()
{
yVelocity = 5;
endYPosition = 1280 + 100;
spawnPosition = new Vector3(400, -100, 0);
gameObject.GetComponent<SpriteRenderer>().color = Constants.RandomColor();
}
// Update is called once per frame
void Update ()
{
MoveObstacle();
RepositionCheck();
}
// why doesn't this work?
private void OnCollisionEnter2D(Collision2D collision)
{
Debug.Log("ahh");
}
void MoveObstacle()
{
gameObject.transform.position += Vector3.up * yVelocity;
}
void RepositionCheck()
{
// check if past reposition point
if (gameObject.transform.position.y >= endYPosition)
{
// reposition
gameObject.transform.position = spawnPosition;
// change color
gameObject.GetComponent<SpriteRenderer>().color =
Constants.RandomColor();
}
}
}
Player object: Obstacle object:
Thank you!
Answer by Raimi · Jun 23, 2018 at 07:13 PM
Try making your rigidbody non-kinematic
Also check out this page. Scroll down to Collision action matrix... https://docs.unity3d.com/Manual/CollidersOverview.html