OnCollisionEnter vs OnTriggerEnter

How do I use them?

In order to use OnTriggerEnter we need to add a rigidbody component to our gameobject/s.

RigidBody added to sphere.

Ticking use gravity will produce similar to below.

In order for us to have the sphere do more than roll off the surface, we need to

attach a script to the gameobject. In the script, if we have:

Then we also need to click on Is Trigger in the collider of the object.

Make sure Is Trigger is ticked.

And the below will happen, instead of rolling off:

Now, if we remove the script, still keeping is trigger ticked the below will happen:

Since there is no script telling the sphere what to do when it encounters another collider it just goes straight through the other object.

Now, if we add a rigidbody component to the rectangle as well, and not have gravity ticked on the rectangle. Also, making sure that Is Trigger is not ticked on both. The below will happen when started.

Then changing the script on the sphere to (similar to above):

The below will happen.

Note inside the brackets of the void also needs to change to Collision.

As can be seen, the sphere adds a force to the rectangle when it touches the rectangle and then transports to its starting position.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Animated FlatList in React Native

Weirdness of JavaScript Functions

Setup Basic Server with Express Framework

A Guide to Component Driven Development (CDD)

Flexbox — 101, 102, and maybe 103

[Action required] Your RSS.app Trial has Expired — Thu Jun 03 2021

Create a lookalike search engine with Next.js, Tailwind and Elasticsearch (10 steps)

Interviewer: You Have Been Working for 3 Years, and You Can’t Answer This Algorithm Question?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jason Schneider

Jason Schneider

More from Medium

VR is the discomfort machine — and that’s a good thing

What does the hype around the SpaceX Human Spaceflight project tell us about the future?

BOOK REVIEW | A SHADOW IN THE EMBER — JENNIFER L. ARMENTROUT

ANALOG THE TIMEGRAPH