Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question check if line hits start and end

Discussion in '2D' started by RobbeBruneel, May 11, 2023.

  1. RobbeBruneel

    RobbeBruneel

    Joined:
    May 5, 2023
    Posts:
    2
    In my game, players have the ability to draw freely, but for the puzzle element, there is a challenge to determine whether the drawn line touches both the starting and ending marks. I need to verify if the line intersects with both the marks. Is this achievable? Your guidance would be appreciated.

    I am interested in a game called 'The Looker' as an example of what I am seeking. This game requires the player to draw lines and succeed if they hit the start and end points. this is the kind of mechanic I am looking for?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,519
    You can use Vector2.Distance() to compute the distance between any two points.

    In your case compare each end of the line to each place it's supposed to be.

    When both are "close enough" (whatever that means in your case), off you go.

    Here's why you must use "close enough" rather than "exactly":

    Floating (float) point imprecision:

    Never test floating point (float) quantities for equality / inequality. Here's why:

    https://starmanta.gitbooks.io/unitytipsredux/content/floating-point.html

    https://forum.unity.com/threads/debug-log-2000-0f-000-1f-is-200.1153397/#post-7399994

    https://forum.unity.com/threads/why-doesnt-this-code-work.1120498/#post-7208431

    "Think of [floating point] as JPEG of numbers." - orionsyndrome on the Unity3D Forums
     
    RobbeBruneel and Lancival like this.
  3. RobbeBruneel

    RobbeBruneel

    Joined:
    May 5, 2023
    Posts:
    2
    Thank you this helped me a lot
     
    Kurt-Dekker likes this.