How to add delay to raycast
I am making a FPS and i want the shots to be delayed .25 seconds, the script I am using to shoot is: using UnityEngine;
public class Gun : MonoBehaviour {
public float damage = 10f;
public float range = 100f;
public Camera FPScam;
public GameObject impactEffect;
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("Fire2"))
{
Shoot ();
}
}
void Shoot ()
{
RaycastHit hit;
if (Physics.Raycast (FPScam.transform.position, FPScam.transform.forward, out hit, range))
{
Debug.Log (hit.transform.name);
Target target =hit.transform.GetComponent<Target>();
if (target != null)
{
target.TakeDamage(damage);
}
GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactGO, 2f);
}
}
} i tried using yield return new WaitForSeconds(.25f); but that just came up with an error, how do I fix this?
Answer by StringAtlas · Dec 27, 2020 at 02:43 AM
I'm going to assume that you did not put the function as an IEnumerator. https://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html
Your answer
Follow this Question
Related Questions
FPS Raycasting vs Instantiate 0 Answers
how can i set the direction of my gun to the Raycast collision point? 1 Answer
How do I make headshots do more damage using raycast? 1 Answer
Avoid using GetComponent to check if RayCast hit is interactable 0 Answers
Help with photon being used to make a mulitplayer fps 0 Answers