- Home /
Question by
Memeparable · Oct 25, 2014 at 09:25 AM ·
raycastrayrays
Raycast cooldown timer?
I need help placing a cool down on a destroy object code I made. I want to make it where the player has to wait X amount of time to be able to destroy another object.
I'm using C# if it wasn't obvious.
using UnityEngine;
using System.Collections;
public class RaycastBreak : MonoBehaviour {
void Update() {
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray,out hit, 5))
{
if(hit.collider.gameObject==gameObject) Destroy(gameObject);
}
}
}
}
Any suggestions or answers are greatly appreciated
Comment
Answer by AlwaysSunny · Oct 25, 2014 at 09:26 AM
How about a co-routine? Place lines 8 through 13 in a new method called TryDestroyWithMouse(){}.
Call TryDestroyWithMouse(); when LMB is down.
void Update() {
// if LMB down, TryDestroyWithMouse();
}
private bool isReady = true;
IEnumerator CoolDown() {
isReady=false;
yield return new WaitForSeconds(1); // wait 1 second
isReady=true;
yield break;
}
void TryDestroyWithMouse() {
if (!isReady) return;
StartCoroutine(CoolDown());
// lines 8 through 13 here
}