How to use Actions/Skill and Animation Events

How to play a animation without coding

You can play animations without code with the Simple Play Motion component in just two steps:

1- Create a Animation State on a Custom Action Layer and assign an animation:

It is recommended to create a copy of the default animator controller to edit it, so if you make a mistake in some edition you can easily return to the original file.

2- Add Simple Play Motion component to your JU Character and configure it:

Simple Play Motion PropertiesDescription

Action Duration

Motion duration

Enter Transition Speed

Enter animation state speed

Exit Transition Speed

Exit animation state speed

Target Layer

Body part to play animation, if you look at the Custom Action Layers of the default Animator Controller, each layer controls a different body part

Animator State Name

Animation state name

Start Motion At

You can cut the beginning of the animation with this option

Input To Call Action

Input to start action

Force Fire Mode

While the action is playing, set FireMode to true

Force No Fire Mode

While the action is playing, set FireMode to false

Block Character Locomotion

While the action is playing, the character does not move

Start Action Even State Is Playing

With this option it is possible to start the action even if it is already playing

Result:

How to create a Action/Skill script

JU TPS has classes and scripts to create actions, emotes or abilities.

The JUTPSAction class is made to interact with the JU Characters system, it has references of JU Character Controller, Animator, Rigidbody, Collider and Camera.

JUTPSAnimatedAction is made for actions involving animations, and has functions to interact with the default JU TPS Animator Controller.

To use these classes you need to make your skill script inherit the classes:

See sample code below:

SkillExample.cs
using UnityEngine;
// > Import JU Input System Lib
using JUInputSystem;

// > inherit the JUTPSAnimatedAction class
public class SkillExample : JUTPSActions.JUTPSAnimatedAction
{
    [Header("Skill Variables")]
    public string AnimationStateName;

    void Start() { SwitchAnimationLayer(ActionPart.FullBody); }

    //Called every frame
    public override void ActionCondition()
    {
        if (JUInput.GetButtonDown(JUInput.Buttons.AimingButton) && IsActionPlaying == false)
        {
            //Start action and play animation
            StartAction();
            PlayAnimation(AnimationStateName);
        }
    }
    //Called every frame while action is playing
    public override void OnActionIsPlaying()
    {
        //Force NO FiringMode
        TPSCharacter.FiringMode = false;
        //Make character fly
        rb.velocity = transform.up * 4;
        //Make the character look in front of the camera
        TPSCharacter.transform.LookAt(cam.transform.forward * 100);
    }

    //Called on action start
    public override void OnActionStarted()
    {
        //Store current item in use
        SetCurrentItemIndexToLastUsedItem();
        //Disable Item
        DisableItemOnHand();
    }
    //Called on action end
    public override void OnActionEnded()
    {
        //Re-equip last used item
        EnableLastUsedItem();
    }
    
    // >> Function to be called by JU Animation Event Receiver <<
    public void MagicAttack()
    {
        Vector3 spawnPos = anim.GetBoneTransform(HumanBodyBones.RightHand).position + transform.forward * 0.3f;
        GameObject magic_attack = Instantiate(MagicAttackParticle, spawnPos, transform.rotation, transform);
        Destroy(magic_attack, 10);
    }
}

This code plays in an animation (line 16 to 21) and makes the character fly and look in front of the camera (line 26 to 31), it also unequips items when the action starts and equips the last used item when the action ends (line 35 to 47). See the result:

The code also has a function to spawn a magic attack at character hand position, but it won't do anything by itself, to make the magic spell attack work, let's go to the next topic.

Animation Event System

It is possible to invoke functions through Animator easily without having to create animation events directly in animations.

1- Add the JU Animation Event to your animation state and give your event a name, and configure at what time the component will call your event(Duration propertie)

2- Add the JU Animation Event Receiver, create an event with the same name you put in the JU Animation Event, and in the Event call the function you want:

See the result:

Last updated