Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Waiting for two mouse position inputs to create a line in runtime

Discussion in 'Scripting' started by danthecoffeecan, Aug 4, 2023.

  1. danthecoffeecan

    danthecoffeecan

    Joined:
    May 26, 2020
    Posts:
    10
    I'm working on a tool for a simulator game that lets the player draw a line (like a road) during runtime. I'm having trouble trying to figure out how to implement this pseudocode.

    Code (CSharp):
    1. //this will be run once from a button OnClick event
    2. void OnLineBuild () {
    3.     Vector3[] startEndPositions = new Vector3[2];
    4.     // On mouse click, get coordindates at mouse position
    5.     // Store mouse position vectors in 2-element vector array
    6.     // Wait for player to give second mouse click at mouse position
    7.     // Store second mouse position vectors as second value in vector array
    8.  
    9.     // While it waits for both mouse clicks or an "esc" button press,
    10.     // Pass vector list to the following class
    11.    SplineFactory.CreateLinear(...);
    12. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
  3. danthecoffeecan

    danthecoffeecan

    Joined:
    May 26, 2020
    Posts:
    10
    Interesting thanks! If I'm reading your code right, you've implemented the two positions interaction as dragging? That might be something I could do, but is there a way to do it on separate mouse clicks?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Yes, see here:

    https://github.com/kurtdekker/makeg...d/makepolygoncollider2d/testfingerpolygons.cs

    See around line 128 where it decides you have dragged far enough and adds the point?

    That would be the logic you would need for each click after the first.

    Obviously the first click logic would be identical to the handling for touch began above.

    Then you have to decide how to indicate that you're done with one shape, moving onto the next. That happens around line 138 where it fabs the geometry and restarts by setting
    Points
    to null.
     
  5. danthecoffeecan

    danthecoffeecan

    Joined:
    May 26, 2020
    Posts:
    10
    Great thank you!