So I have a system where 2 scripts control all interactions, I have my mouse script that uses a raycast to get the GameObject it collides with name and if it is an InteractionBox then it enabled the InteractionInfo script which handles it all
In Interaction there are a few booleans that tell it what scripts are in its parents so it knows what to do once it is activated
The problem is… the way that it works means that when I click the interaction box on a spawner for example it will spawn 2-3 times instead of just once?
public class NMouseController : MonoBehaviour {
public float LookAtSpeed;
public bool Firing;
private InteractionInfo interactionInfo;
void Start () {
}
void FixedUpdate () {
if (Firing == false) {
Invoke("LookAtMouse", 0);
}
if (Input.GetButton ("CommandKey") && Input.GetButton ("FireKey")) {
Firing = true;
Invoke("TargetDetails", 0);
}
if (!Input.GetButton ("CommandKey") && Input.GetButton ("FireKey")) {
Invoke("ClickDetect", 0);
}
}
void ClickDetect(){
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
Debug.DrawRay(ray.origin, ray.direction * 100, Color.yellow, 10);
RaycastHit hit;
if (Physics.Raycast (ray, out hit)) {
string HitObj = (hit.transform.gameObject.name);
if (HitObj == "InteractionBox"){
interactionInfo = hit.transform.gameObject.GetComponent<InteractionInfo>();
interactionInfo.enabled = true;
}
}
}
And this is my Interaction script that is called
public class InteractionInfo : MonoBehaviour {
public Vector3 RayHitPos;
public bool SpawnScript;
public bool EnemyAIScript;
public bool MovementScript;
private SpawnButton spawnButton;
private GameObject GameObj;
void Start(){
GameObj = transform.parent.gameObject;
this.enabled = false;
}
void Update(){
if (SpawnScript == true) {
Invoke ("ActivateSpawn", 0);
}
}
void ActivateSpawn(){
spawnButton = GameObj.GetComponent<SpawnButton>();
spawnButton.Spawn = true;
this.enabled = false;
}
}
I assume I would go about using Time.time somehow to stop it registering 1 click in 2-3 frames