A classic tutorial Translated From DT: Introduction to unity.
Trigger usage in game production is very convenient and practical. This document briefly introduces how to use a simple trigger to automatically open and close the door.
First, make sure that you have set the animation for the door.
The specific process is as follows.
Choose WINDOW> animation to open the animation window. Select the animation door to be added and click the red button to create a new animation.
After selecting an animation element (such as postion. X), click the Add key frame button:
Drag the timeline and let go at the animation end point:
Drag the door to the position you want to stop in the scene to create the key frame of the animation end point:
Click the keys on the animation panel to preview the door start effect.
At this point, a simple animation is completed.
The next step is to create a trigger.
First, add a gameobject-> creaateother-> sphere object, and use it as a trigger.
After creation, remove the selected meshrender to make the sphere transparent:
Add a built-in unity script to this sphere to enable the trigger function:
Select the target object of the trigger:
Set the active event to animate:
However, at this time, the sphere is a collision body and cannot be rushed into to implement the trigger function. Therefore, you need to check the is trigger option:
When we run it again, we will find that the door will automatically open when we approach it.
The source code of the project is too large to be uploaded. You can send me an email if necessary.