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 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