Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
442 views
in Technique[技术] by (71.8m points)

c# - Why does OnCollisionEnter not get triggered?

I have got two objects:

Object A (SteamVR Camera):

  • Has a Rigidbody with "Is Kinematic" enabled.
  • Has a Box Collider with "Is Trigger" enabled.
  • Has the Script with the "OnCollisionEnter" method.

Object B (Globe model):

  • Has a Sphere Collider with "Is Trigger" enabled.

I made sure that the collider ranges are correctly, but somehow the OnCollisionEnter method does not get triggered.

The code that I am using for OnCollisionEnter looks like below:

private void OnCollisionEnter(Collision collision)
{
    Debug.Log("Collision entered");
}

Could someone provide me some insight/advice?

Thanks in forward.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

ISSUE: there's no collision between any object with a Trigger

To understand how collision works you need first to identify which objects are colliding, because very few objects may trigger OnCollisionEnter.

From your description we can identify 2 types of objects.

Object A: Kinematic Rigidbody Trigger Collider
Object B: Static Trigger Collider

If you check the Collision Matrix there could be no collision with anything that has a Trigger (both Object A and Object B), so OnCollisionEnter won't fire.
I repeat: a Trigger does not collide with anything no matter what the other object is.

SOLUTION: Use OnTriggerEnter instead of OnCollisionEnter

If you want to get the event from a collision with a trigger objects (or 2 of them, like in your case) you can use OnTriggerEnter.

Consider that in this case the parameter of the method will be Collider other that is a Collider instead of a Collision, but in your case I think you won't need the collision at all.

private void OnTriggerEnter(Collider other) 
    => Debug.Log("entered");

INSIGHTS about Unity collisions

The collisions depend on the rigidbody and collider setup objects. I can show a few examples based on your situation.

The first element to consider is that Static Collider is not an usual Object set as Static (this was a source of confusion in the comments below).

From Unity Manual

STATIC COLLIDER
This is a GameObject that has a Collider but no Rigidbody. Static colliders are used for level geometry which always stays at the same place and never moves around. Incoming rigidbody objects will collide with the static collider but will not move it.

Consider also that you want to use a trigger (that makes your STATIC COLLDER into a STATIC TRIGGER COLLIDER).

Basically the idea behind trigger is: "Use triggers when you want to catch the event but you don't want the gameobject interacting with Physics forces."

Triggers

The scripting system can detect when collisions occur and initiate actions using the OnCollisionEnter function. However, you can also use the physics engine simply to detect when one collider enters the space of another without creating a collision. A collider configured as a Trigger (using the Is Trigger property) does not behave as a solid object and will simply allow other colliders to pass through. When a collider enters its space, a trigger will call the OnTriggerEnter function on the trigger object’s scripts


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...