- Home /
How to make a certain function if an object collides with another object? Been searching it up for 2 months.
idk what is wrong with the code:
void OnCollisionEnter(Collision col)
{
Debug.Log("Collided");
}
it doesn't work for some reason. And I also need to check "if (col.collider.gameObject.tag == "red")", but I can't as it doesn't even send the message to the console, it doesn't work.
Some details:
One of my objects is set to dynamic, but the other one - to kinematic. I'm using Unity UI and a canvas. That script is on the dynamic object.
I've been searching for solutions, reading a lot of posts for a little over 2 months now. Even took a break for 1 month cause this is just cancer. Should I just suicide at this point lol
Did you read the documentation about kinematic parameter???
If is$$anonymous$$inematic is enabled, Forces, collisions or joints will not affect the rigidbody anymore
So obviously, the collision messages won't be sent.
Without mentionning that UI elements are not meant to use Physics engine. If you really need it, use the 2D Physics engine by using 2D colliders, 2d rigidbodies and the 2D callback functions.
well,
"Note: Collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached."
It said one of the colliders, but I'll try try to remove another one also, thanks for the reply
EDIT: Still didn't work.
Are your colliders "is Trigger"? One "is trigger" and another one is collider? Are these objects in layers that match in Physics Table? ( Edit > Project Settings->Physics)
Read carefully: https://docs.unity3d.com/$$anonymous$$anual/CollidersOverview.html Section Collision action matrix
Thanks for the reply. Yup one "is trigger" dynamic, another default dynamic and both layers are UI. Still doesn't work.
Answer by JonPQ · Dec 18, 2018 at 08:11 PM
both objects must have rigid bodies attached, and at least one must be non kinematic. also try putting your script on both objects... see if only one is getting the message. Also try adding OnCollisionStay() with a log message in case they are already colliding.
See unity docs https://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html
also try this tutorial https://unity3d.com/learn/tutorials/topics/physics/detecting-collisions-oncollisionenter
Thanks for the reply. Both have rigid bodies, both are dynamic, I tried putting ontriggerenter; ontriggerstay; oncollisionenter; oncollisionstay on all of my obj, but it still didn't work.
you say both layers are Ui... just a thought.... are you trying to collide them in 2d ? view the scene in 3d mode.. make syre they are at same Z and colliding in all 3 dimensions...
...also check your edit->project settings->physics settings... it has a bunch of check-boxes showing which layers will collide with other layers... You might have Ui disabled.
also why not grab some free demos/learning resources with collisions working... plug in your models... check what is different. https://unity3d.com/learn/tutorials/s/physics
Your answer
Follow this Question
Related Questions
Player registering collisions where there are none 0 Answers
Flying Controls Collision Issues 0 Answers
Why there is no Collider.IsTouching(...) ? 2 Answers
How does Unity detect collision? 0 Answers
More realistic physics? 0 Answers