Using Animation Events to throw an object

Now that your animated character is ready, you might want to coordinate some of her actions with her animation states. In this recipe, we will exemplify this by making the character throw an object whenever the appropriate animation clip reaches the right time. To do so, we will make use of Animation Events, which basically trigger a function from the animation clip's timeline. This feature, recently introduced to the Mecanim system, should feel familiar to those experienced with the Add Event feature of the classic Animation panel.

Using Animation Events to throw an object

Getting ready

For this recipe, we have prepared a Unity Package named Throwing, containing a basic scene that features an animated character and a prefab named EasterEgg. The package can be found inside the 1362_07_07 folder.

How to do it...

To make an animated character throw an Easter egg (!), follow these steps:

  1. Create a new project and import the Throwing Unity Package. Then, from the Project view, open the mecanimPlayground level.
  2. Play the level and press F on your keyboard. The character will move as if she is throwing something with her right hand.
  3. From the Project view, create a new C# Script named ThrowObject.cs.
  4. Open the script and add the following code:
    using UnityEngine;
    using System.Collections;
    
    public class ThrowObject : MonoBehaviour {
      public GameObject prop;
      private GameObject proj;
      public Vector3 posOffset;
      public Vector3 force;
      public Transform hand;
      public float compensationYAngle = 0f;
        
      public void Prepare () {
    
        proj = Instantiate(prop, hand.position, hand.rotation) as GameObject;
        if(proj.GetComponent<Rigidbody>())
          Destroy(proj.GetComponent<Rigidbody>());
        proj.GetComponent<SphereCollider>().enabled = false;
        proj.name = "projectile";
        proj.transform.parent = hand;
        proj.transform.localPosition = posOffset;
        proj.transform.localEulerAngles = Vector3.zero;
      }
    
      public void Throw () {
    
        Vector3 dir = transform.rotation.eulerAngles;
        dir.y += compensationYAngle;
        proj.transform.rotation = Quaternion.Euler(dir);
        proj.transform.parent = null;
        proj.GetComponent<SphereCollider>().enabled = true;
        Rigidbody rig = proj.AddComponent<Rigidbody>();
        Collider projCollider = proj.GetComponent<Collider> ();
        Collider col = GetComponent<Collider> ();
        Physics.IgnoreCollision(projCollider, col);
        rig.AddRelativeForce(force);
      }
    }
  5. Save and close the script.
  6. Attach the ThrowObject.cs script to the character's GameObject named MsLaser.
  7. Select the MsLaser object. From the Inspector view, check out its Throw Object component. Then, populate the Prop field with a prefab named EasterEgg. Populate Hand with mixamorig:RightHand. Also, change Pos Offset to X: 0; Y: 0.07; Z: 0.04. Finally, change Force to X: 0; Y: 200; Z: 500.
    How to do it...
  8. From the Project view, select the Swat@toss_grenade file. Then, from the Inspector view, access the Animation section and scroll down to the Events section.
  9. Expand the Events section. Drag the playhead to approximately 0:17 (017.9%) of the animation timeline. Then, click on the button with the marker + icon to add an Animation Event. From the Edit Animation Event window, set Function as Prepare. Close the window.
    How to do it...
  10. Add a new animation event at approximately 1:24 (057.1%) of the animation timeline. This time, from the Edit Animation Event window, set Function as Throw. Close the window.
  11. Click on the Apply button to save the changes.
  12. Play your scene. Your character will now be able to throw an Easter egg when you press the F key.

How it works...

Once the toss_grenade animation reaches the moments that we have set our Events to, the Prepare() and throw() functions are called. The former instantiates a prefab, now named projectile, into the character's hand (Projectile Offset values are used to fine-tune its position), also making it respect the character's hierarchy. Also, it disables the prefab's collider and destroys its Rigidbody component, provided it has one. The latter function enables the projectile's collider, and adds a Rigidbody component to it, making it independent from the character's hand. Finally, it adds a relative force to the projectile's Rigidbody component, so it will behave as if thrown by the character. The Compensation YAngle can be used to adjust the direction of the grenade, if necessary.

..................Content has been hidden....................

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