Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question How to sync vfx to specific attack animator (ARPG)

Discussion in 'Documentation' started by chen90166, May 5, 2024.

  1. chen90166

    chen90166

    Joined:
    Jun 8, 2021
    Posts:
    2
    UNITY Version 2022.3.27f1 LTS

    before starting I'll first attach the instructional video I used

    Combat


    Vfx




    i want to know how to sync Slash vfx to Attack 1 , Attack 2 , Attack 3 animator

    play Slash vfx when Attack 1 (or 2 3) play , not just KeyMouse0 respectively play Attack 1 and Slash vfx





    Maybe there is some related script that I use

    Attack Control

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class AttackControl : MonoBehaviour
    {
    private Animator anim;
    private void Awake()
    {
    anim = GetComponent<Animator>();
    }

    private void Update()
    {
    if(Input.GetKeyDown(KeyCode.Mouse0))
    {
    anim.SetTrigger("Attack");
    }
    }
    }



    SlashTest

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class SlashTester : MonoBehaviour
    {
    public Animator anim;
    public List<Slash> slashes;

    private bool attacking;

    // Start is called before the first frame update
    void Start()
    {
    DisableSlashes();
    }

    // Update is called once per frame
    void Update()
    {
    if (Input.GetKeyDown(KeyCode.Mouse0) && !attacking)
    {
    attacking = true;
    anim.SetTrigger("Attack");
    StartCoroutine(SlashAttack());
    }
    }

    IEnumerator SlashAttack()
    {
    for (int i = 0; i < slashes.Count; i++)
    {
    yield return new WaitForSeconds(slashes[i].delay);
    slashes[i].slashObj.SetActive(true);
    }

    yield return new WaitForSeconds(1);
    DisableSlashes();
    attacking = false;
    }

    void DisableSlashes()
    {
    for (int i = 0; i < slashes.Count; i++)
    slashes[i].slashObj.SetActive(false);
    }
    }

    [System.Serializable]

    public class Slash
    {
    public GameObject slashObj;
    public float delay;
    }

     
  2. Pixelith

    Pixelith

    Joined:
    Jun 24, 2014
    Posts:
    585
    So normally, you could use animation events to set a slash active during a certain part of the animation. This is the easiest and most accessible method to you. This method is limited as you can't really preview how the vfx will look until you play test the game.
     
    chen90166 likes this.
  3. chen90166

    chen90166

    Joined:
    Jun 8, 2021
    Posts:
    2
    Now I have another problem... :confused:

    I successfully applied vfx to three attacks animation event

    and create new two vfx (slash2 , slash3)


    But these three attacks (attack1 , attack2 , attack3) only use slash1

    like this
    【attack1 <- slash1】
    【attack2 <- slash1】
    【attack3 <- slash1】

    but i want
    【attack1 <- slash1】
    【attack2 <- slash2】
    【attack3 <- slash3】

    how can i do this in script? (or event?)



    Slash Tester2

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.Experimental.VFX;
    using UnityEngine.VFX;

    public class SlashTester2 : MonoBehaviour
    {
    public VisualEffect slashObj;

    public void EnableSlash(int isEnable)
    {

    if (isEnable == 1)
    {
    slashObj.Play();
    }
    }
    }
     
  4. Pixelith

    Pixelith

    Joined:
    Jun 24, 2014
    Posts:
    585
    Change your script a bit and you'll have it.
    Change
    public VisualEffect slashObj;
    to
    public VisualEffect[] slashObj;
    . This changes it to an array where you can define multiple slashes. Now in your EnableSlash event, you can use that int to instead get rid of the if statement and say
    slashObj[isEnable].Play();
    . This way you can can enable any slash you want from a single method.

    It's a little.much to take in for someone new. Just make sure to look up arrays if you get confused as to why it works this way. You could also define them in a list but I think you should go one step at a time.
     
    chen90166 likes this.