我的Coroutine将行不通。我该如何修复?

发布于 2025-02-12 14:24:36 字数 835 浏览 1 评论 0原文

我正在尝试制作游戏,但是当我编码方面时,它无效。因此,基本上,当您进入扳机时,咖啡杯应掌握在您的手中并跟随您。我试图使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

装纯掩盖桑 2025-02-19 14:24:37

考虑将咖啡杯育儿到手对象。如果您没有手对象,请将一个空的游戏对象添加到一个孩子的角色中,然后将其放在手部位置。

当您进入扳机时,将杯子在手对象下方并设置为vector3.zero

public Transform hand;
public Transform coffee;
void OnTriggerEnter(Collider collider)
{
    coffee.SetParent(hand);
    coffee.localPosition = 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

public Transform hand;
public Transform coffee;
void OnTriggerEnter(Collider collider)
{
    coffee.SetParent(hand);
    coffee.localPosition = Vector3.zero;
}
画离情绘悲伤 2025-02-19 14:24:37

由于两个原因,您有问题:

  • 不将位置设置为对象。您的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.positionupdate> update> update> update方法中
  • )在他的回答中提到,您可以将杯子从小时候添加到手中,然后将其自动移动。

实际上,您应该决定要实现的目标,以便能够为您的情况选择正确的选项。

You have your issue because of two reasons:

  • not setting the position to an object. Your pos value is just a position, not the position of the object. You need to assign the new position to the cup's gameObject.transform.position.
  • actually, even if you will assign a new position to your object, it will not move at all, as you always set the same position to it (new Vector3(pos.x,0.813f,pos.x) will always have the same value)

So, you have at least two options:

  • sync cup position with the hand position (you can use collider variable in the OnTriggerEnter method to get the target position by using collider.gameObject.transform.position, store it, and sync your cup gameObject.transform.position with it in the Update method)
  • as Everts mentioned in his answer, you can add your cup as a child to the hand, then it will move with the hand automatically.

Actually, you should decide what you want to achieve to be able to choose the correct option for your case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文