0
public Rigidbody2D rb;
public Rigidbody2D hook;
public float releaseTime = 0.15f;

private bool isPressed = false;

void Update()
{
    if (isPressed)
    {

        Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

        if (Vector3.Distance(mousePos, hook.position) > 2.5f)
        {
            rb.position = hook.position + (mousePos - hook.position).normalized * 2.5f;
        }
        else
        {
            rb.position = mousePos;
        }
    }
}

void OnMouseDown()
{
    isPressed = true;
    rb.isKinematic = true;
}

void OnMouseUp()
{
    isPressed = false;
    rb.isKinematic = false;

    StartCoroutine(Release());
}

IEnumerator Release()
{
    yield return new WaitForSeconds(releaseTime);
    GetComponent<SpringJoint2D>().enabled = false;
    this.enabled = false;
}

I have the script above, that allows me to attach an object to a springJoint2D and fire it, something like Angry Birds. Is there a possibility to make this script work on mobile devices?

  • 1
    It should work on mobile but it's better to use the event system. Since you are using a 2D collider you need to use **#7** from the answer in the duplicated question. `OnPointerUp` and `OnPointerDown` are the appropriate functions. – Programmer Jul 11 '17 at 12:38
  • @Programmer Thank you, I'll give it a try. – Michael Jul 11 '17 at 12:42

0 Answers0