How to do it...

To create a simple Action Point, follow these steps:

  1. Import the character to Unity.
  2. Put it on the scene and add a NavMesh Agent component to it.
  3. Bake the NavMesh in the scene.
  4. Create an Animator Controller and assign it to the character's Animator component.
  5. Create a float Speed parameter and a Trigger Action parameter in the controller.
  1. Drag and drop the IdleWalk, and Action animations to the controller. Make sure that the Idle animation is the default state.
  2. Create four transitions (see the following image):
    • IdleWalk with one condition: Speed parameter greater than 0.5. Has Exit Time should be set to false and TransitionDuration set to around 0.1 seconds.
    • WalkIdle with one condition: Speed parameter less than 0.5. Has Exit Time should be set to false and Transition Duration set to around 0.1 seconds.
    • Any StateAction with one condition: Action triggers parameter. Has Exit Time should be set to false and Transition Duration set to around 0.1 seconds.
    • ActionIdle with no conditions: Has Exit Time should be set to true and Transition Duration set to around 0.1 seconds.
Animator Controller with Action state used by the Action Point
  1. Create a new script and call it ActionPoint.cs.
  2. In that script, we have an IEnumerator PerformAction() coroutine that handles the Action Point usage (it is started in the Start() function). In this coroutine, we first check if our character is close enough to the Action Point. If not, we set the NavMesh Agent's destination to the Action Point's position, wait one frame, and check again:
        while ((agentTransform.position - 
        transform.position).sqrMagnitude > actionDistance * 
        actionDistance) 
        { 
            agent.SetDestination(transform.position); 
            agent.Resume(); 
            yield return null; 
        }  
  1. The agentTransform variable holds the reference to the character's transform. The actionDistance is the distance from the Action Point in which our character should start performing the action.
  2. When our character is closer to the Action Point than the actionDistance, we stop the NavMesh Agent and check if we want to match the character's position and/or rotation to our Action Point before the character starts playing the action animation. If not, we start playing the animation right away:
        agent.Stop(); 
        if (!matchBeforeAction) 
        { 
            anim.SetTrigger(actionTrigger); 
        } 
  1. Next we check if we want to match character's position and/or rotation with Action Point's position/rotation. If so, we use linear interpolation to match the position and/or rotation. If the position and/or rotation of the character are close enough to the Action Point's position/rotation, we set the character's position/rotation to be exactly the same as Action Point's position/rotation:
        while (matchRotation == true || matchRotation == true) 
        { 
            yield return null; 
            if (matchPosition && (agentTransform.position - 
            transform.position).sqrMagnitude > 0.01f) 
            { 
                agentTransform.position = 
                Vector3.Lerp(agentTransform.position, 
                transform.position, Time.deltaTime * 
                lerpSpeed); 
            } 
            else 
            { 
                matchPosition = false; 
                agentTransform.position = 
                transform.position; 
            } 
            if (matchRotation && 
            Vector3.Angle(agentTransform.forward, 
            transform.forward) > 1f) 
            { 
                agentTransform.rotation = 
                Quaternion.Lerp(agentTransform.rotation, 
                transform.rotation, Time.deltaTime * 
                lerpSpeed); 
            }  
            else 
            { 
                agentTransform.rotation = 
                transform.rotation; 
                matchRotation = false; 
            } 
        } 
  1. Lastly, we check if we want to play the animation after the character's position and/or rotation was matched with the Action Point. If so, we play the animation now (as the character position and/or rotation was adjusted):
        if (matchBeforeAction) 
        { 
            anim.SetTrigger(actionTrigger); 
        } 
  1. Save the script.
  2. Create a new empty game object, call it Action Point, and place it in the scene, where the character should perform the action.
  3. Attach the script to the Action Point game object. Drag and drop the character to the Agent field of the script's component.
  4. You may need to adjust the Action Distance value of the script. You can also set the Match PositionMatch Rotation, and Match Before Action options to achieve different results.
  5. Play the game to see the effect.
..................Content has been hidden....................

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