Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Instantiate spell projectile only after 2 second casting animation completes

Discussion in 'Editor & General Support' started by cmitchell1313, Oct 15, 2020.

  1. cmitchell1313

    cmitchell1313

    Joined:
    Oct 13, 2020
    Posts:
    6
    Hi there, I'm new to unity programming and would love some help with this problem on my first RPG! I have a character that casts spells on left button click and right button click. Presently, the projectiles instantiate instantly upon button click, however the character model I am using has a 2 second "casting" type animation, and I would like to instantiate the projectile at the moment in time that the animation reaches its 'punch point' at 1.5s in (3/4 the way through the animation runtime). Everything works currently in the code below, except there is no delay before the projectile is released and it looks, well, bad.


    using UnityEngine;

    public class WepB : MonoBehaviour
    {
    [SerializeField]
    [Range(0.1f, 1.5f)]
    private float fireRate = 0.3f;
    // cooldown time

    [SerializeField]
    [Range(1, 10)]
    private int damage = 1;

    // 7 muzzle animation elements
    [SerializeField]
    private ParticleSystem muzzleParticleZ;
    [SerializeField]
    private ParticleSystem muzzleParticleY;
    [SerializeField]
    private ParticleSystem muzzleParticleX;
    [SerializeField]
    private ParticleSystem muzzleParticleW;
    [SerializeField]
    private ParticleSystem muzzleParticleV;
    [SerializeField]
    private ParticleSystem muzzleParticleU;
    [SerializeField]
    private ParticleSystem muzzleParticleT;

    // instantiated gameobject
    [SerializeField]
    private GameObject spellToCastB;

    // audio on casting
    [SerializeField]
    private AudioSource castSoundSourceB;

    // cooldown timer
    private float timer;


    private void Update()
    {
    timer += Time.deltaTime;
    if (timer >= fireRate)
    {

    if (Input.GetButton("Fire2"))

    {
    timer = 0f;
    Debug.Log("Fired right click");
    UseMuzzle();
    UseWepB();
    }
    }
    }

    // instantiates muzzle particles and sound
    private void UseMuzzle()
    {
    muzzleParticleZ.Play();
    muzzleParticleY.Play();
    muzzleParticleX.Play();
    muzzleParticleW.Play();
    muzzleParticleV.Play();
    muzzleParticleU.Play();
    muzzleParticleT.Play(); was
    castSoundSourceB.Play();
    }



    // below is where the spell is instantiated, and needs a 1.5s delay before it executes

    private void UseWepB()
    {

    Ray rayTwo = Camera.main.ViewportPointToRay(Vector3.one * 0.5f);
    Debug.DrawRay(rayTwo.origin, rayTwo.direction * 100, Color.green, 2f);

    Instantiate(spellToCastB, transform.position, Quaternion.LookRotation(rayTwo.direction * 100));


    // damage system
    RaycastHit hitInfo;
    if (Physics.Raycast(rayTwo, out hitInfo, 50))

    {
    var health = hitInfo.collider.GetComponent<Health>();
    if (health != null)
    health.TakeDamage(damage);
    }
    }
    }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,724
  3. cmitchell1313

    cmitchell1313

    Joined:
    Oct 13, 2020
    Posts:
    6
    it worked! The second link detailed the script - I wasn't aware i could call the coroutine within the update function which allowed it call at the proper instance. Cheers