Detecting Collisions with Collectibles

Tutorial

·

Beginner

·

+10XP

·

15 mins

·

(5491)

Unity Technologies

Detecting Collisions with Collectibles

In this tutorial, you’ll:

  • Revise your PlayerController script to make PickUpGameObjects disappear when they collide with the Player sphere
  • Tag PickUp GameObjects and write conditional statements to make sure they’re the only things that disappear
  • Set the Prefab PickUp Collider as a trigger and add a Rigidbody component, to make the collectibles work properly

Languages available:

1. Overview

In this tutorial, you’ll:


  • Revise your PlayerController script to make PickUpGameObjects disappear when they collide with the Player sphere

  • Tag PickUp GameObjects and write conditional statements to make sure they’re the only things that disappear

  • Set the Prefab PickUp Collider as a trigger and add a Rigidbody component, to make the collectibles work properly


By the end of this tutorial, your game will look something like this:



2. Disable PickUps with OnTriggerEnter

Follow the video or text instructions below to add the OnTriggerEnter function and set the other object as inactive inside that function:



Download Transcript


1. Add the OnTriggerEnter function.


  • Open the PlayerController script for editing.

  • Beneath the FixedUpdate function, add the following code:

[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

2. Set the other object as inactive.


  • Add the following line of code inside the function body for OnTriggerEnter:

[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

Note: Nothing new will happen in the game yet, since the PickUp GameObjects are not yet set up appropriately as triggers.


3. Add a tag to the PickUp Prefab

Follow the video or text instructions below to edit the PickUp prefab, create a new tag, and apply the tag to the prefab:



Download Transcript


1. Edit the PickUp prefab.


  • In the Project window, go to the Prefabs folder and select the PickUp prefab.

  • Any changes you make to the prefab when you select it in the Project window will apply to all prefabs.

2. Create a new tag.


  • At the top of the Inspector window, from the Tag dropdown menu, select Add Tag.

  • Select the Add (+) button to add a new tag, then name it “PickUp”.

Important: This is case sensitive, so be careful — it needs to be exactly the same spelling and capitalization that you use in the script later on.


3. Apply the tag to the prefab.


  • With the PickUp prefab still selected, use the Tag dropdown menu to select the new “PickUp” tag from the list.

  • Double-check that the tag has been applied to all pickups in the scene by selecting them and making sure they have the “PickUp” tag applied.


4. Write a conditional statement

Follow the video or text instructions below to check for the “PickUp” tag, disable the objects if that condition is met, and save your script:



Download Transcript


1. Check for the “PickUp” tag.


  • Open the PlayerController script.

  • At the top of the function body for OnTriggerEnter, add the following IF statement:

[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

2. Disable the objects if that condition is met.


  • Cut and paste the line of code you already had into that IF statement:

[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

3. Save your script.


  • Make sure to save your script before returning to Unity.

  • Your entire OnTriggerEnter function should now look like this:

[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

5. Set the Pickup Colliders as triggers

Follow the video or text instructions below to change the pickups to triggers and test your game:



Download Transcript


1. Change the pickups to triggers.


  • In the Prefabs folder, select the PickUp prefab.

  • In its Box Collider component, enable IsTrigger.

  • Double-check that this change has been applied to the PickUp GameObjects in the Scene view.


2. Test your game.


  • Save the scene, then enter Play mode to test it.

  • As the Player GameObject enters the trigger, the PickUp GameObjects should disappear!


6. Add a Rigidbody component to the PickUp Prefab

Follow the video or text instructions below to add a Rigidbody component to the PickUp GameObjects, but disable physics calculations for optimization purposes:



Download Transcript


1. Add a Rigidbody component to the PickUp GameObjects.


  • In the Project window, go to the Prefabs folder and select the PickUp prefab.

  • In the Inspector window, select Add Component and add a Ridigbody component.

2. Disable physics calculations.


  • In the Rigidbody component, disable Use Gravity and enable Is Kinematic.

  • Save the scene again and enter Play mode to test to make sure the game is still behaving as expected.


Optional Step

7. Final script sample

If your script is not working as expected, below is an example of what your code should look like at this point. The comments have been added to make the code more readable.


PlayerController.cs


[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

Complete this tutorial