- Home /
How add click area?
hi, im making a project and you need to throw a ball with swipe but my code have a touch phase so when i click in pause menu my code detects like im touching the ball and the ball drops, i have another code and i make a ball click area with a collider so you need to touch in the collider to shoot i want make the same but with this code, i tried with the same forms with collider but doesnt work.
if ( Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
// getting touch position and marking time when you touch the screen
touchTimeStart = Time.time;
startPos = Input.GetTouch(0).position;
}
// if you release your finger
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended && throwAllowed)
{
// marking time when you release it
touchTimeFinish = Time.time;
// calculate swipe time interval
timeInterval = touchTimeFinish - touchTimeStart;
// getting release finger position
endPos = Input.GetTouch(0).position;
// calculating swipe direction
direction = startPos - endPos;
// add force to ball rigidbody depending on swipe time and direction
rb.isKinematic = false;
rb.AddForce(-direction / timeInterval * throwForse);
// one attempt to throw a ball only
throwAllowed = false;
}
If i understood you right then you want your collider NOT to take touches when in pause mode right?
Answer by tormentoarmagedoom · Sep 07, 2018 at 06:59 AM
You can simply make a boll variable called "pause" and activate it when you are in pause.
The at the touch detection code, just commence with a
if (!pause).....
Bye
@tormentoarmagedoom i tried that but doesnt work i put a bool variable ballClicked and if touch collider the ballClicked is true and if is true dont do anything
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if (hit.collider != null)
{
if (hit.collider.gameObject.name == ballClick.gameObject.name)
{
ballIsClicked = true;
}
else
{
ballIsClicked = false; //Don't start activating the shot
}
}
if ( Input.touchCount > 0 && ballIsClicked == false && Input.GetTouch(0).phase == TouchPhase.Began)
{
// getting touch position and marking time when you touch the screen
touchTimeStart = Time.time;
startPos = Input.GetTouch(0).position;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612174203im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Input.getTouch() vs checking if button is clicked 1 Answer
Detect swipe along 3d arrows 1 Answer
mouse input to touch input help please 0 Answers
swipe movement control 1 Answer