简体   繁体   English

启动协程后如何 go 回到脚本?

[英]How to go back to script after starting Coroutine?

I currently have a script that allows the player to pick up an item and plays an animation.我目前有一个脚本,允许玩家拾取物品并播放 animation。 I have a coroutine to wait 1 second before locking the game object to the player's hand.在将游戏 object 锁定到玩家手中之前,我有一个协程等待 1 秒。 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)让我知道是否需要更多信息(堆栈溢出仍然是新手)

Forget about the coroutine and use Time.time to save the time of pickup to a class variable.忘记协程并使用 Time.time 将拾取时间保存到 class 变量。 Then add comparison to the other if branches such as Time.time > pickupTime + 1f .然后将比较添加到另一个 if 分支,例如Time.time > pickupTime + 1f

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM