0
\$\begingroup\$

This code works but it has a little delay on computer controls and higher delay on mobile touch screen controls, so its not responsive as I want it to be.

void OnMouseDown()
{
    offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, transform.position.y, 0.0f));
}

void OnMouseDrag()
{
    Vector3 newPosition = new Vector3(Input.mousePosition.x, transform.position.y, 0.0f);
    transform.position = Camera.main.ScreenToWorldPoint(newPosition) + offset;
    transform.position = new Vector2(Mathf.Clamp(transform.position.x, posXMin, posXMax), transform.position.y);
}
\$\endgroup\$
2
  • \$\begingroup\$ This code alone does not create a delay, so the lack of responsiveness is likely coming from somewhere else in your project. \$\endgroup\$ Commented Dec 23, 2020 at 15:01
  • \$\begingroup\$ Ok, I'll have to look into that. Thank you :) \$\endgroup\$ Commented Dec 23, 2020 at 18:24

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.