"Dropping object"

I want to make a game where the player is a helicopter and there is a small flat sphere under the player.
If it collides with a “Crate”, it carries it by setting the sphere as the parent for the “Crate”.

But how do you release it? I want to hit the Y key and the object fall.
It does not fall however. It still follows and remains a child of the sphere.

This is what I am doing currently:

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

public class extendPickup : MonoBehaviour
{

public GameObject gameobject;
public GameObject homePosition;
public GameObject deployPosition;

bool release = false;

// Use this for initialization
void Start()
{

}

// Update is called once per frame
void Update()
{

    if (Input.GetKey(KeyCode.G) == true)
    {
        transform.position = Vector3.MoveTowards(transform.position, deployPosition.transform.position, .1f);
    }
    else if (Input.GetKey(KeyCode.T) == true)
        transform.position = Vector3.MoveTowards(transform.position, homePosition.transform.position, .1f);

    if (transform.position == deployPosition.transform.position)
        transform.localScale = deployPosition.transform.localScale;
    else
        transform.localScale = homePosition.transform.localScale;

    if (Input.GetKeyDown(KeyCode.Y) == true)
        release = true;

}

void OnTriggerEnter(Collider other)
{

    if (other.gameObject.tag == "Grabable")
    {
        if (release == false)
        {

            other.gameObject.transform.parent = transform;
        }
        if (release == true)
            other.gameObject.transform.parent = null;
    }
}

}

1 Answer

1

void OnTriggerEnter() only executes the code once everytime you collide with something. You should instead use update and put something like if (Input.GetKeyDown(KeyCode.Y) == true) { Transform myChild = transform.GetChild (1); if (myChild != null) myChild.parent = null; }

,OnTiggerEnter () only executes the code once (when the object collides with something). If you want it to release when you press the button you should put other.gameObject.transform.parent = null; in update under if (Input.GetKeyDown(KeyCode.Y) == true).