Hey guys! I’ve been having this bug for a while now. An if statement within my FixedUpdate calls gets called at random times when it doesn’t even meet the conditions.
This is the statement:
if (hit.collider != null && hit.collider.tag == “bug” && Input.GetTouch (0).phase == TouchPhase.Began && hitcheck == true && Main.GameOver == false) {
if (hit.transform.gameObject.renderer.enabled == true) {
Main.TotalApples = Main.TotalApples + 1;
Main.ApplesEarned = Main.ApplesEarned + 1;
}
bugishit = true;
StopCoroutine (Currency ());
StartCoroutine (Currency ());
hitcheck = false;
}
This is the full method:
void FixedUpdate ()
{
if (Input.touchCount >= 1) {
hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint ((Input.GetTouch (0).position)), Vector2.up);
//On touch
if (hit.collider != null && hit.collider.tag == "bug" && Input.GetTouch (0).phase == TouchPhase.Began && hitcheck == true && Main.GameOver == false) {
if (hit.transform.gameObject.renderer.enabled == true) {
Main.TotalApples = Main.TotalApples + 1;
Main.ApplesEarned = Main.ApplesEarned + 1;
}
bugishit = true;
StopCoroutine (Currency ());
StartCoroutine (Currency ());
hitcheck = false;
}
if (Input.GetTouch (0).phase == TouchPhase.Ended && hitcheck == false) {
hitcheck = true;
}
}
}
I notice when I play the game that even if I don’t hit or tap on a bug, the prior if statement still gets called. And it does this at random times. Any help is greatly appreciated! Thanks
**EDIT: YES! I found the problem, but I am at a total loss on how to fix it.
My raycast points up to infinity, so even if i tap under a bug it collides with it.
hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint ((Input.GetTouch (0).position)), Vector2.up);
anybody know any way for fixing this? Thanks in advance =D**