Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Calculating distance from center of a circle(e.g. archery board)

Discussion in 'Physics' started by sami1592, Dec 10, 2014.

  1. sami1592

    sami1592

    Joined:
    Sep 18, 2013
    Posts:
    50
    Suppose i have a 2d archery game(silly, i know). Player's job is to tap the moving circular archery board. Depending of the accuracy of the tap the player will get a score. More the center is the tap, the more is the score.
    So, after determining if the tap is within the circle I have to calculate the tap accuracy(by accuracy i mean how close is the tap from the center of the circle)


    So, how to calculate the distance from the center of the circle to a specific point?
    circle.png .
     
  2. dterbeest

    dterbeest

    Joined:
    Mar 23, 2012
    Posts:
    382
    Vector2.Distance(center, le_random_point);
    or
    Vector3.Distance(center, le_random_point);
     
  3. sami1592

    sami1592

    Joined:
    Sep 18, 2013
    Posts:
    50
    tnx, i know that part. but how can i know my tap is in the circle or not?
     
  4. MikeUpchat

    MikeUpchat

    Joined:
    Sep 24, 2010
    Posts:
    848
    But that was your question, how to calculate the distance :) Clearly if the distance is greater than the radius of your circle the tap is outside, if less it is inside.
     
    NareshKhandla likes this.
  5. cl9

    cl9

    Joined:
    May 31, 2013
    Posts:
    417
    You could do something such as worldPosition = camera.ScreenToWorldPoint(tapPosition) where tapPosition's z component is equal to the target's z component. Then you could get the distance between the target's center and the tap point using Vector3.Distance(tapPosition, targetPosition).