Object is pulled up but never down

Hello,

I am trying to make an object move up and out of bounds when I release it, and then coming down again. Its basically a fishing hook going up with the fish and then coming back down into the water. The problem I am having is it goes up (different how far up every time) and never comes down again.

    IEnumerator hookOff ()
    {
        transform.position = Vector3.MoveTowards(transform.position, hookOffPos, Time.deltaTime * 2);
        yield return new WaitForSeconds(2);
        transform.position = Vector3.MoveTowards(transform.position, startPos, Time.deltaTime);
    }

What am I doing wrong? startPos is set in the start of the scene, with the hook in the middle of the water. But the hook never returns here. Its like it is getting stuck on the way to the hookOffPos.

Respectfully Ellen

In this code nothing looks wrong. Show the code what calls this.

1 Like
    void Update()
    {
        if (hookMovement == true)
        {
            StartCoroutine(hookOff());
        }
}
    void OnMouseUp()
    {
        hookMovement = true;
    }
    void OnMouseDown()
    {
        hookMovement = false;
        offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
    }

It’s like the two movetowards are working against each other, like the first one never reaches its target

Solved it :smile:

    void Update()
    {
        if (hookMovement == true)
        {
            if (hookUp == true)
            {
                transform.position = Vector3.MoveTowards(transform.position, hookOffPos, Time.deltaTime * 2);
                if (Vector3.Distance(transform.position, hookOffPos) < 0.1f)
                {
                    hookUp = false;
                    hookDown = true;
                }
            }
            if (hookDown == true)
            {
                transform.position = Vector3.MoveTowards(transform.position, startPos, Time.deltaTime * 2);
                if (Vector3.Distance(transform.position, startPos) < 0.1f)
                {
                    hookDown = false;
                }
            }
        }
}