协程启动后如何回到脚本?
How to go back to script after starting 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将取车时间保存到class变量。然后添加与其他 if 分支的比较,例如 Time.time > pickupTime + 1f
.
我目前有一个脚本可以让玩家拿起一个物品并播放动画。我有一个协程等待 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将取车时间保存到class变量。然后添加与其他 if 分支的比较,例如 Time.time > pickupTime + 1f
.