[SOLVED] release the gameobject from grapling hook

i modified the follow script to attach objects to my grapling hook, when it collides with them, but i can’t figure out how to properly release them when i press right click. i am trying to change the target to my nohand gameobject’s transform, but it only works if i have not yet hit any other object.
anyone of you see a fix for it?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Grab : MonoBehaviour
{
    private Animator anim;
    public Vector3 offset = new Vector3(0f, 0f, 3f);
    public Transform target;
    public Transform nohand;
    //nohand is a empty gameobject
    public Transform glove;
    public int distance = 3;

    void Start()
    {
        Vector3 playerPos = glove.transform.position;
        Vector3 playerDirection = glove.transform.forward;
        Quaternion playerRotation = glove.transform.rotation;
        anim = GetComponent<Animator>();
    }

    void OnCollisionEnter(Collision FindTarget)
    {
        if (FindTarget.gameObject.tag == "Enemy" || FindTarget.gameObject.tag == "Rabbit")
        {
            target = FindTarget.transform;
        }
    }
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            anim.SetTrigger("grab");
        }
        if (target != null)
        {
            target.transform.position = glove.transform.position + offset;
            target.transform.forward = glove.transform.forward + offset;
            target.transform.rotation = glove.transform.rotation;
        }
        else if (Input.GetMouseButtonDown(1))
        {
            target = nohand;
        }
    }
}

btw. thanks you to alexander for helping me with the sound earlier in case you read this

        if (target != null)
        {
...
        }
        else if (Input.GetMouseButtonDown(1))
        {
...
        }

In a nutshell, this is what your code looks like. As long as target != null, you can never reach the GetMouseButtonDown(1) check.

oh that’s why, it is working now.
before i asked the question i tried to read the sequence aloud, i guess i didn’t do properly :slight_smile:
well case closed, thank you BlackPete for your help.

can i tag this thread as solved in any way?

Nah, you could change the title to include [Solved] or something, but otherwise it’s all good. :slight_smile: