B
B
Bulat1112022-03-05 15:34:24
Unity
Bulat111, 2022-03-05 15:34:24

How to throw an object on a trigger?

The player carries the object. In case of a collision in another script, the object disappears and errors appear in insp.

// 1 коллизия
 if (col.tag == "Thing")
        {          
            if (gameObject.tag == "fountain") 
            {               
            Destroy(fountbase);           
            Instantiate(probka, probkaspawn.position, Quaternion.identity);
            wma.WaterStopper();           
            }


//2  коллизия

 void OnTriggerEnter2D(Collider2D col)
    {
        if (col.tag == "fountain" || col.tag == "fountain2" || col.tag == "fountain3" || col.tag == "fountain4" || col.tag == "fountain5" || col.tag == "fountain6" || col.tag == "fountain7" || col.tag == "fountain8" || col.tag == "fountain9")
        {
            Instantiate (cover, CoverSpawner.position, Quaternion.identity);
            Destroy(cover);

//здесь получается ошибка

 void FixedUpdate()
    {
        if (Input.GetKeyDown(KeyCode.F))
        {
            if (!hold)
            {
                Physics2D.queriesStartInColliders = false;
                hit = Physics2D.Raycast(transform.position, Vector2.right * transform.localScale.x, distance);
 
                if (hit.collider != null && hit.collider.tag == "Thing")
                {
                    hold = true;
                }
            }
            else
            {
                hold = false;
 
                if (hit.collider.gameObject.GetComponent<Rigidbody2D>() != null)
                {
                    hit.collider.gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(transform.localScale.x, 0.3f) * throwObject;
                }
            }
 
        }
        if (hold)
       {
           hit.collider.gameObject.transform.position = holdPoint.position;   //[COLOR="Red"]вот здесь ошибка[/COLOR]
          
         if (holdPoint.position.x > transform.position.x && hold==true)
           {
                hit.collider.gameObject.transform.localScale = new Vector2(transform.localScale.x * 8f, transform.localScale.y * 8f);
            }
            else if (holdPoint.position.x < transform.position.x && hold == true)
            {
                hit.collider.gameObject.transform.localScale = new Vector2(transform.localScale.x * 8f, transform.localScale.y * 8f);
            }
        }
             
 
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question