Playing unique particle systems for melee attack combos

Grannyboy

Active member
I got some cool sword slash vfx laying around that I want to use with my melee weapon. The trail system dosent really work at all with spawning specific particle systems so I made this script with help from @ChristianWiele!

This is very basic but its very easy to build upon. The concept works around the animator audio part of the melee weapon script. More on combo set ups etc is in the tutorial on melee weapons from Opsive:

First of all set a state name and a empty state with that name in the melee script:
1652365216434.png

Then add a new script on your character:
1652365254322.png


In the script put the following code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Opsive.Shared.Events;
using Opsive.UltimateCharacterController.Items.Actions;

public class StartSwordSlash_G : MonoBehaviour
{
[SerializeField] private ParticleSystem swordSlashOneParticle;

public void Awake()
{
EventHandler.RegisterEvent<GameObject, string, bool> ("OnStateChange", OnStateChange);
}

private void OnStateChange(GameObject gameObject, string stateName, bool active)
{
if (stateName == "SwordSlashOne" && active)
{
Debug.Log("SwordSlash one initiated");
swordSlashOneParticle.Play();
}
}

public void OnDestroy()
{
EventHandler.RegisterEvent<GameObject, string, bool>("OnStateChange", OnStateChange);
}

}

(Please tell me how to write to get the above code into a code box)

I have my VFX placed as a child of my character, but place it where ever suits your VFX best.

Result:
swordslice.gif
 
I ran into a problem as I was expanding on this concept. Maybe @Justin you can see what causes this issue.

I made 4 different states with the 4 different Animator audio states on the melee weapon: SwordSlashOne-SwordSlashFour
1652455150714.png

I made a simple if statement to check in which state Im currently at:
1652455200908.png

The issue here is when I keep hitting and the sequence is incrementing i start to get duplicates. See the console for this:
1652455286424.png

It registers as follows:
First hit: 1
Second hit : 1 & 2
Third hit: 2 & 3
Fourth hit: 3 & 4
etc

See this video for clarification:


Note that I also tried to manually set the state to false in the co routines but to no avail.

Is there something obvious im missing here?
 
I found out a better way to do this! Im not sure if I should discard this thread or simply continue... For simplicity I just continue here.
I abandoned the state based system and tried the event driven system instead. I have NO IDEA if this solution is good for the performance or scalability of the project, but thats another issue.

My goal is still to play unique particle systems for each unique hit I have in my sequence. I got 4 hits so far in my sequence cycle and it looks like this:
1652638409959.png
Very simple.

Now to the cool part, in my animator I have my 4 attack animations:
1652638478085.png

On my animation I added an event the exact moment I want my particle system to play:
1652638540555.png

(Im continuing in next post due to the limitation of 3 pictures in each post)
 
Top