【问题标题】:How to go back to script after starting Coroutine?启动协程后如何返回脚本?
【发布时间】:2021-12-30 18:25:21
【问题描述】:

我目前有一个脚本,允许玩家拾取物品并播放动画。在将游戏对象锁定到玩家的手之前,我有一个协程等待 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;
            
            
        }
    }


}

如果需要更多信息,请告诉我(堆栈溢出仍然是新手)

【问题讨论】:

  • 离题,但您可能想从.NET 4 with Unity 2018+ 签出async/await 而不是使用协程
  • 我不知道您所说的“返回脚本”是什么意思。 When you use your debugger 并单步执行 Update,尤其是在 if 语句中,变量与您的预期有何不同?

标签: c# unity3d coroutine


【解决方案1】:

忘记协程并使用 Time.time 将拾取时间保存到类变量中。然后添加与其他 if 分支的比较,例如 Time.time &gt; pickupTime + 1f

【讨论】:

  • 谢谢,我查看了 Time.time,但这不是我想要的。但是,这导致我找到 Invoke("nameofvoid",1);.
猜你喜欢
  • 2020-11-05
  • 2021-12-19
  • 2018-11-03
  • 1970-01-01
  • 1970-01-01
  • 2020-02-03
  • 1970-01-01
  • 2018-05-29
  • 1970-01-01
相关资源
最近更新 更多