Search Unity

Drag object between two points with mouse

Discussion in 'Scripting' started by Zilk, Dec 18, 2014.

Thread Status:
Not open for further replies.
  1. Zilk

    Zilk

    Joined:
    May 10, 2014
    Posts:
    333
    I have a slider where I want to move the handle between two points with the mouse/touch.

    The handle is rotated to always face the points so if I could move it in x (localposition) towards the closest point to the mouse and clamp it to not travel outside the two points I would be a happy camper.

    Anyone got any ideas how to achieve this, this is what I got so far since I can't figure out how to move it in a good way.

    Code (csharp):
    1.  
    2.         Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    3.         mousePos = new Vector3(mousePos.x,mousePos.y,0f);
    4.  
    5.         target.rotation = Quaternion.LookRotation(Vector3.forward, sliderLeft.position - sliderRight.position);
    6.         target.Rotate(0f, 0f, -90f);
    7.         target.position = Vector3.MoveTowards(target.position, mousePos, Time.deltaTime * 5f);
    8.  
     

    Attached Files:

  2. Zilk

    Zilk

    Joined:
    May 10, 2014
    Posts:
    333
  3. Zilk

    Zilk

    Joined:
    May 10, 2014
    Posts:
    333
    I actually managed to translate the flash script and get it working. So here it is if anyone wants to do something similar sometime.
    Code (csharp):
    1.  
    2. public transform target; //The draggable buttons transform
    3. public transform sliderLeft; //The sliders left endpoint
    4. public transform sliderRight; //The sliders right endpoint
    5.  
    6.     public void UpdateSlider() {
    7.         float vX = 0f;
    8.         float vY = 0f;
    9.         float dX = 0f;
    10.         float dY = 0f;
    11.         float iX = 0f;
    12.         float iY = 0f;
    13.         float dotV = 0f;
    14.         float t = 0f;
    15.  
    16.         Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    17.  
    18.         vX = sliderRight.position.x - sliderLeft.position.x;
    19.         vY = sliderRight.position.y - sliderLeft.position.y;
    20.  
    21.         dX = mousePos.x - sliderLeft.position.x;
    22.         dY = mousePos.y - sliderLeft.position.y;
    23.  
    24.         dotV = vX * dX + vY * dY;
    25.         t = dotV / (vX * vX + vY * vY);
    26.         t = Mathf.Clamp(t, 0f, 1f);
    27.  
    28.         iX = sliderLeft.position.x + vX * t;
    29.         iY = sliderLeft.position.y + vY * t;
    30.    
    31.         target.position = Vector3.MoveTowards(target.position, new Vector3(iX, iY, 0f), Time.deltaTime * 5f);
    32.     }
    33.  
    34.  
     
  4. mehializade

    mehializade

    Joined:
    Oct 9, 2020
    Posts:
    2
    I've been looking for this for days and there was not a single person solving this! Thanks a lot!
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    Please, use the like button to show your appreciation, it's what it's for! Nobody needs to see a 8 year old thread necroed like this. It's also against the forum rules.

    Thanks.
     
    Niter88 likes this.
Thread Status:
Not open for further replies.