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();
if (health != null)
health.TakeDamage(damage);
}
}
}