This post has been wikified, any user with enough reputation can edit it.
2D trigger and mouse button help
Hey! I am basicly just a begginer in programing a I have a little bit of a problem. In my game there is a ball bouncing around and a box in the centre. The player must try to click at the same time as the ball is in the centre. In any other scenario, he loses(he clicks when it isnt or he doesn't when it is). This is how I tried to archieve that for now:
function OnTriggerEnter2D ()
{
ballOnCenter = 1;
}
function OnTriggerExit2D ()
{
ballOnCenter = 0;
}
function Update ()
{
CheckPoint();
}
function GivePoint()
{
points += 1;
}
function CheckPoint()
{
if(Input.GetMouseButtonDown(0) && ballOnCenter == 1)
{
GivePoint();
}
else if(Input.GetMouseButtonDown(0) && ballOnCenter == 0)
{
Time.timeScale = 0.0; //for now I'm just pausing the game instead of loading the game over scene
}
else if(ballOnCenter == 1)
{
Time.timeScale = 0.0;
}
}
The problem is, that it is now really hard to click in just the right time. If you're a milisecind too late, you've already lost... Does anyone have a better idea on how to do this?
Comment
Never $$anonymous$$d... Solved it. Here is how it looks like now:
#pragma strict
var ballOnCenter : int;
var points : int = 0;
var clicked : boolean;
function OnTriggerEnter2D ()
{
ballOnCenter = 1;
}
function OnTriggerExit2D ()
{
ballOnCenter = 2;
}
function Update ()
{
CheckPoint();
if (clicked == true && ballOnCenter == 2)
{
GivePoint();
}
else if (clicked == false && ballOnCenter == 2 || Input.Get$$anonymous$$ouseButtonDown(0) && ballOnCenter == 0)
{
Time.timeScale = 0.0;
}
}
function GivePoint()
{
points += 1;
clicked = false;
ballOnCenter = 0;
}
function CheckPoint()
{
if(Input.Get$$anonymous$$ouseButtonDown(0) && ballOnCenter == 1)
{
clicked = true;
}
}