3
\$\begingroup\$

I have a circle sprite that i only want to move between 2 empty game objects (points). Using Vector3.Lerp() I'm able to move the circle between those points

void FixedUpdate()
{
    float speed = 10 * Time.deltaTime;
    float current = Mathf.PingPong(Time.time * speed, 1.0f);
    transform.position = Vector3.Lerp(startPoint.position, endPoint.position, current);
}

The output: What I currently have with lerp

The problem:

I want to move that circle using a mouse drag (basically like a UI slider from point a to point b) and as soon as the mouse leaves the circle to stop moving it

I can currently move the circle anywhere on the screen using the mouse. I've set a circleCollider2D around the circle sprite and I have this code that moves the circle

bool Selected = false;
void Update()
{
    if (Selected)
    {
        Vector2 cursorPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        transform.position = cursorPos;
    }

    if (Input.GetMouseButtonUp(0))
        Selected = false;
}

void OnMouseOver()
{
    if (Input.GetMouseButton(0))
        Selected = true;
}

What I've tried:

  • I have tried adding a gameobject with a box collider between the two points and using triggers to detect when the circle exits the trigger (OnTriggerExit2D), but this solution only works when the circle fully leaves the collider (which i don't want). I want a way for the circle to move just like its moving currently with Vector3.Lerp but with mouse control

  • I tried locking the circle to only one axis and moving it. While this worked, it failed when i rotated the whole slider as its only locked on one axis and rotation doesn't change that

  • I have also tried to implement the mouse position into the Vector3.Lerp function with no luck Vector3.Lerp(startPoint.position, endPoint.position, mousePosition.x);

    I tried with y and z, all of it failed. And this way wouldn't work when i go to rotate the slider

\$\endgroup\$
  • \$\begingroup\$ Hi, can you explain a bit more about the ball behaviour and the mouse ? If I understand correctly : circle follow mouse at a lower speed than mouse is moving, so if I move my mouse very fast somewhere, ball will move slowly (at constant speed ?) until it reaches it ? And not go out of a specified space ? \$\endgroup\$ – Sebastien Servouze May 29 at 10:16
  • \$\begingroup\$ @SebastienServouze not at all. Think of it this way, let's say I want it to be a progressbar where the ball is x% the way between point a and point b. I want that ball to only be able to move in a straight line between the 2 points while following the mouse movement \$\endgroup\$ – SenaCarbtc May 31 at 2:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.