啟動Coroutine後如何返回腳本?

編碼_鵝

我目前有一個腳本,允許玩家拿起一個項目並播放動畫。我有一個協程等待 1 秒鐘,然後將游戲對象鎖定到玩家的手上。我的問題是我用於丟棄和扔出物品的代碼不再起作用。是否有解決此問題的解決方法?

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;
            
            
        }
    }


}

讓我知道是否需要更多信息(堆棧溢出仍然是新手)

呸呸呸

忘記協程,使用 Time.time 將拾取時間保存到類變量中。然後添加與其他 if 分支的比較,例如Time.time > pickupTime + 1f.

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

啟動後燒瓶運行腳本

如何通過命令啟動屏幕並運行python腳本

啟動到 py 腳本然後結束

從 Python 2.7 腳本啟動 XAMPP 控制面板 Apache MySql

模塊未導入在啟動時運行 python 腳本

讓製作cdk exec的ec2像Dockerfile一樣啟動腳本

加載第一個腳本後如何使腳本執行

如何從腳本輸出文本或返回對象?

有沒有辦法每次雙擊使用我的 PowerShell 編譯腳本啟動文件?

根據類名的字符串動態啟用/禁用 GameObjects 腳本組件

${@:3} shell 腳本後如何接受參數

我如何使用 python 後端部署 php 腳本

導入後如何運行腳本?

如何從另一個 Python 腳本訪問 Python 腳本的動態變量?

如何在無效輸入後返回上一條語句,而不是在 Java 中重新啟動整個程序?

批處理腳本完成後如何關閉命令窗口

Laravel 8,如何在後台運行一個作業(腳本)?

如何啟動在 FastAPI 中提供響應後不關閉的後台進程

如何從 bash 執行動態生成的 perl 腳本?

如何為谷歌網絡應用程序腳本進行自動對焦?

如何將變量傳遞給驅動程序執行腳本?

應用程序啟動後如何繼續製作動畫?

如何在運行腳本之前提交在活動單元格中所做的更改?(谷歌表格/谷歌應用腳本)

LUA - 如何停止運行腳本

如何減慢滑塊腳本

如何啟動不包含返回的包含函數的協程?

服務器啟動後如何運行測試

如何在一段時間後啟動代碼?

如何將消息表單 shell 腳本返回到 Ansible playbook 以顯示為輸出