Home > Software design >  How to go back to script after starting Coroutine?
How to go back to script after starting Coroutine?

Time:11-21

I currently have a script that allows the player to pick up an item and plays an animation. I have a coroutine to wait 1 second before locking the game object to the player's hand. My problem is that my code for dropping the item and throwing it no longer functions. Is there a workaround to solve this problem?

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

public class PickUp : MonoBehaviour
{
    bool isgrounded = true;
    public Animator animator;
    public GameObject Player;
    public Rigidbody rb;
    public bool inrange;
    public int number = 1;
    public Transform theDest;
    public float thrust = 1.0f;
    public float upthrust = 1.0f;


    void start()
    {
        rb = GetComponent<Rigidbody>();
        inrange = false;
    }
    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.name == "Player")
        {
            inrange = true;
        }
    }
    private void OnTriggerExit(Collider other)
    {
        inrange = false;
    }


    public void Update()
    {
        bool isPickUp = animator.GetBool("isPickUp");
        bool pickuppressed = Input.GetKey("e");
        if (isgrounded == true)
        {
            if (pickuppressed && !isPickUp && inrange==true)
            {
                animator.SetBool("isPickUp", true);
            }
            if (!pickuppressed && isPickUp || inrange == false)
            {
                animator.SetBool("isPickUp", false);
            }

            if (Input.GetKeyUp(KeyCode.E) && (number % 2) == 1 && inrange == true )
            {

                StartCoroutine(waiter());
                number = number   1;
            }
             else if (Input.GetKeyUp(KeyCode.E) && (number % 2) == 0 && inrange == true)
            {
                GetComponent<Rigidbody>().isKinematic = false;
                GetComponent<BoxCollider>().enabled = true;
                this.transform.parent = null;
                GetComponent<Rigidbody>().useGravity = true;
                number = number - 1;
            }
             else if (Input.GetKey(KeyCode.G) && (number % 2) == 0 && inrange == true)
            {
                GetComponent<Rigidbody>().isKinematic = false;
                GetComponent<BoxCollider>().enabled = true;
                this.transform.parent = null;
                GetComponent<Rigidbody>().useGravity = true;
                number = number - 1;
                rb.AddForce(Player.transform.forward * thrust);
                rb.AddForce(Player.transform.up * upthrust);
            }
        }
        void OnCollisionEnter(Collision theCollision)
        {
            if (theCollision.gameObject.name == "floor")
            {
                isgrounded = true;
            }
        }

        //consider when character is jumping .. it will exit collision.
        void OnCollisionExit(Collision theCollision)
        {
            if (theCollision.gameObject.name == "floor")
            {
                isgrounded = false;
            }
        }
        IEnumerator waiter()
        {
            yield return new WaitForSeconds(1);
            GetComponent<BoxCollider>().enabled = false;
            GetComponent<Rigidbody>().useGravity = false;
            GetComponent<Rigidbody>().isKinematic = true;
            this.transform.position = theDest.position;
            this.transform.parent = GameObject.Find("Destination").transform;
            
            
        }
    }


}

Let me know if more information is needed (still new to stack overflow)

CodePudding user response:

Forget about the coroutine and use Time.time to save the time of pickup to a class variable. Then add comparison to the other if branches such as Time.time > pickupTime 1f.

  • Related