Refactoring environment interactions to 3D API methods
From the previous 2D game projects, we have a small library of components built up already, but they are 2D, and we are now in 3D, so it requires a bit of refactoring for us to use them. Let’s first revisit the TriggeredEvent
component we created in Chapter 4.
Revisiting the TriggeredEvent component
The Physics 2D API method we used for the TriggeredEvent
component is OnTriggerEnter2D()
. Its 3D counterpart simply drops the 2D part, and so it’s just OnTriggerEnter()
(Unity is 3D under the hood, and it would make sense that only the 2D-specific methods are indicated as such).
Now, considering the above, let’s look at how we’ll update the TriggeredEvent
code. Provided that you copied in the Assets/Scripts/TriggeredEvent.cs
file from the previous 2D project, only a few changes are required. Otherwise, you can first revisit the earlier code in the book or even download the 2D version of the script...