How to do it...

To create and assign an animated moving platform, follow these steps:

  1. Select your platform game object in the Hierarchy.
  2. Open the Animation View.
  3. Make sure the record button is pressed (in the upper-left corner of the Animation View).
  4. Move the platform to the desired position in the first frame.
  5. Adjust the timeline a few seconds forward and move your platform to new destination.
  1. To make the animation loop, select the first key frame and press Ctrl + C (cmd C on Mac). Then adjust the timeline forward from the second key frame, the same amount of seconds like the previous step. If the first key frame is on the 0 seconds mark and the second on the 10 seconds mark, set the timeline to 20 seconds mark. Press Ctrl V to paste the copied key frame. In our example, the platform is going from one floating island to another and back. It also has some pauses on each end to make it easier for the player to jump on to it.
  2. When you are happy with your animation, exit the Animation View.
  3. An Animator Controller will be created and an Animation Clip will be assigned to it automatically. Also, an Animator component will be added to the platform game object.
  4. Find the Animator Controller on the platform game object and set the Update Mode property to Animate Physics. Set the Culling Mode to Always Animate.
  5. Add a Rigidbody component to the platform and select the Kinematic checkbox.
  6. Add a collider component (a Mesh Collider or a Box Collider) to your platform. If you are using a Mesh Collider, make sure to select the Convex checkbox.
  7. Create a new script, and name the file Platform.cs.
  8. Open the script and write the following code:
      using UnityEngine; 
      using System.Collections; 
 
      public class Platform : MonoBehaviour { 
 
       /*This function is called by Unity every time this object 
       starts to collide with any other game object with a Collider 
       component attached.The Collision collisionInfo object 
       parameter stores the information about the collision and the 
       object we are colliding with.*/ 
 
          void OnCollisionEnter(Collision collisionInfo) 
          { 
        /*We are checking if the object we are colliding with 
        has a RigidBody component and the RigidBody is not set 
        to kinematic. Optionally we can also check the tag of 
        the object we are colliding with here (to make it work 
        only for the player for instance).*/ 
         
              if (collisionInfo.rigidbody != null 
              && !collisionInfo.rigidbody.isKinematic) 
              { 
         
            /*We are setting the parent of the object we are 
            colliding with to the platform game object (the 
            object out script is attached to).This will make 
            our character move with the platform instead of 
            slide from it.*/ 
         
                  collisionInfo.transform.parent = transform; 
 
              } 
           } 
 
    /*This function is called by Unity every time this object stop 
    colliding with any object with a Collider component attached. 
    The CollisionInfo collision info parameter stores the same 
    information as in the OnCollisionEnter function.*/ 
 
          void OnCollisionExit(Collision collisionInfo) 
          { 
 
        /*We are checking the same conditions as before*/ 
 
              if (collisionInfo.rigidbody != null 
              && !collisionInfo.rigidbody.isKinematic) 
              { 
            /*We are setting the parent of the object we are                 colliding with to null. The object has no parent 
            at all and stops moving with the platform*/ 
             
                  collisionInfo.transform.parent = null; 
              } 
          } 
      } 
  1. Attach the script to the platform game object and play the game to see the effect.
  2. This moving platform will work with characters using Rigidbody components to move. You can import the ThirdPersonCharacter prefab from Unity's Standard Assets. You can also write your own simple character movement. To do so, see the There's more section.
..................Content has been hidden....................

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