See if objects overlap when user taps the screen
I have a simple game where a line bounces up and down with a point zone in the middle of the screen and when the line overlaps the point zone, the user taps the screen to get a point. I have been able to set up a program that prints to the console repeatedly when they overlap but when I add in the condition that the user is touching/clicking the screen, it no longer works. I will tap the screen very quickly when the line is clearly overlapping and it will only print to the console maybe once, usually after the line is no longer overlapping.
Here is the class for my point zone:
public class PointZone: MonoBehaviour
{
Transform y1;
Transform y2;
float h1;
float h2;
// Start is called before the first frame update
void Start()
{
y1 = gameObject.GetComponent<Transform>();
y2 = GameObject.Find("Line").GetComponent<Transform>();
h1 = gameObject.GetComponent<RectTransform>().rect.height;
h2 = GameObject.Find("Line").GetComponent<RectTransform>().rect.height;
}
// Update is called once per frame
void Update()
{
//if(Input.touchCount > 0)
//{
if(Input.GetMouseButtonDown(0) == true) {
Debug.Log("gug");
float min1;
float max1;
float min2;
float max2;
min1 = y1.position.y - (h1 / 2);
max1 = y1.position.y + (h1 / 2);
min2 = y2.position.y - (h2 / 2);
max2 = y2.position.y + (h2 / 2);
if (min2 <= min1 && min1 <= max2)
{
Debug.Log("overlap");
}
if (min2 <= max1 && max1 <= max2)
{
Debug.Log("overlap");
}
}
}
}
Your answer
Follow this Question
Related Questions
raycast mouse detects any object 1 Answer
Physics2D.OverlapCircle doesnt work 1 Answer
Toggle between positions 0 Answers
HOW TO MOVE A OBJECT TO ANOTHER OBJECT SMOOTHLY NOT INSTANTLY? 0 Answers
Highscore not working 0 Answers