How it works...

This recipe illustrates a very important feature of Unity's animation workflow, the possibility to animate objects in local space. If we animate child game objects, their position, rotation, and scale will be animated in relation to their parent game objects. This makes it possible to create animated game objects, save them as prefabs, and reuse them in our games.

There are a few key elements of this recipe:

  • Door as a child of Door Frame: We have two objects: an animated, moving Door and a stationary Door Frame. The Door game object is the child of the Door Frame game object in the Hierarchy. If we animate it, an Animator component will be added to the Door Frame game object (the parent), and the Door (the child) will be animated relative to the Door Frame (the parent).
  • Bool parameter "Open": This parameter is used by the door Animator Controller to switch between Opened and Closed states. We set it in the script attached to the trigger game object. When player enters the trigger, it sets the Open parameter to true, which tells the Door Animator Controller it should play the Door Opening animation. When the player exits the trigger, it sets the Open parameter to false, so the Door Animator Controller plays the Door Closing animation.
  • Animation-driven movement: The Door game object is animated to open (move up) and close (move down), depending on the bool Open parameter. The Door game object has a collider to prevent any Rigid body going through it. It also has a Rigidbody component set to Is Kinematic. All moving colliders should be kinematic Rigid bodies for optimization reasons.
  • Trigger game object: The Door Frame has also a trigger game object as a child. A trigger is a game object with a collider set to Is Trigger. It recognizes when an object with a Rigidbody component enters or exits the collider and calls the OnTriggerEnter and OnTriggerExit functions, respectively. We set the value of the bool Open parameter in those functions.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.104.100