我的Coroutine将行不通。我该如何修复?
我正在尝试制作游戏,但是当我编码方面时,它无效。因此,基本上,当您进入扳机时,咖啡杯应掌握在您的手中并跟随您。我试图使用coroutine,然后循环执行此操作。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GrabCoffee : MonoBehaviour
{
// Start is called before the first frame update
public Vector3 pos;
public GameObject coffee;
void Awake()
{
pos = coffee.transform.position;
}
void Start()
{
}
// Update is called once per frame
void Update()
{
}
IEnumerator grabbing()
{
while (true)
{
yield return new WaitForSeconds(0.1f);
pos = new Vector3(pos.x,0.813f,pos.x);
}
}
void OnTriggerEnter(Collider collider)
{
StartCoroutine(grabbing());
}
}
当我与此相撞时,我只是经历了它,什么也没有发生
I am trying to make a game but when i was coding an aspect it didnt work. So basically, when you enter the trigger, the coffee cup should be in your hands and follow you. I tried to use a coroutine and while loop to do this.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GrabCoffee : MonoBehaviour
{
// Start is called before the first frame update
public Vector3 pos;
public GameObject coffee;
void Awake()
{
pos = coffee.transform.position;
}
void Start()
{
}
// Update is called once per frame
void Update()
{
}
IEnumerator grabbing()
{
while (true)
{
yield return new WaitForSeconds(0.1f);
pos = new Vector3(pos.x,0.813f,pos.x);
}
}
void OnTriggerEnter(Collider collider)
{
StartCoroutine(grabbing());
}
}
when I collide with this i just go through it and nothing happens
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
考虑将咖啡杯育儿到手对象。如果您没有手对象,请将一个空的游戏对象添加到一个孩子的角色中,然后将其放在手部位置。
当您进入扳机时,将杯子在手对象下方并设置为vector3.zero
Consider parenting the coffee cup to the hand object. If you don't have a hand object, add an empty game object to the character as child and place it at the hand position.
When you enter the trigger, parent the cup under the hand object and set its position to Vector3.zero
由于两个原因,您有问题:
pos
值只是一个位置,而不是对象的位置。您需要将新位置分配给Cup的gameObject.transform.position
。new vector3(pos.x,0.813f,pos.x,pos.x
)将始终具有相同的值),因此,您至少有两个选项:
Collider
collider 变量中 intriggerenter 方法通过使用collider.gameobject.transform.position
,存储并同步您的杯子gameObject.transform.position.position
在update> update> update> update
方法中实际上,您应该决定要实现的目标,以便能够为您的情况选择正确的选项。
You have your issue because of two reasons:
pos
value is just a position, not the position of the object. You need to assign the new position to the cup'sgameObject.transform.position
.new Vector3(pos.x,0.813f,pos.x
) will always have the same value)So, you have at least two options:
collider
variable in theOnTriggerEnter
method to get the target position by usingcollider.gameObject.transform.position
, store it, and sync your cupgameObject.transform.position
with it in theUpdate
method)Actually, you should decide what you want to achieve to be able to choose the correct option for your case.