查找并返回最近的带有标签的游戏​​对象,Unity [英] Find and return nearest gameObject with tag, Unity

查看:34
本文介绍了查找并返回最近的带有标签的游戏​​对象,Unity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一款游戏,您必须合并 2 个游戏对象才能获得下一个等等.首先,我尝试这样当游戏对象被释放并且它接触另一个游戏对象时,它会删除这两个对象,然后实例化下一个.但这在触发覆盖"的情况下并没有完全奏效.彼此或其他一些奇怪的但是.所以我想我可以检测到碰撞,这样每个接触触发器的游戏对象都会被添加到一个列表中,然后在退出时被删除.我不想弄清楚的是如何计算游戏对象之间的距离,如果它们足够接近(合并阈值),那么它们都会被破坏并且下一个游戏对象会被实例化,但我无法弄清楚如何获得这个距离计算和返回什么游戏对象.所以所有的帮助将不胜感激!(不要遗漏任何细节,因为我是初学者)谢谢!

im trying to make a game where you have to merge 2 gameObjects to get the next one and so one. First i tried so that when the gameobject is released and it's touching another gameobject it deletes both of those and then instantiates the next one. But this didn't quite work bc of the triggers "covering" each other or some other wierd but. So i thought i could detect the collision so that every gameobject that touches the trigger gets added to a list and then removed on exit. What i'm not trying to figure out is how to calculate the distance between the gameObjects and if they are close enough (mergingthreshold) then they both get destryed and the next gameobject gets instantiated, but i can't quite figure out how to get this distance calculating and returning of what gameObject that is. So all help would be appreciated! (don't leave out any details, cause I'm quite the beginner) Thanks!

这是我目前得到的代码:

Here's the code I've gotten so far:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Merging : MonoBehaviour
{
    List<GameObject> NearGameobjects = new List<GameObject>();


    void Start()
    { 
    }

    void Update()
    {
    }

    void OnTriggerEnter2D(Collider2D col)
    {
        if (col.gameObject.tag == gameObject.tag)
        {
            Debug.Log("Enter!");
            NearGameobjects.Add(col.gameObject);
        }
    }


    void OnTriggerExit2D(Collider2D col)
    {
        if (col.gameObject.tag == gameObject.tag)
        {
            Debug.Log("Exit!");
            NearGameobjects.Remove(col.gameObject);
            
        }
    }
}

推荐答案

如果您打算使用该列表并将所有对象添加到该列表中,您可以使用 for each loop 并检查主要对象和列表中的所有其他对象(如果它更接近)将其添加到壁橱对象

If you're going to use the list and have added all objects into this list you could use a for each loop and check the distance between your main object and all the other objects in the list if its closer add that to the closets object

    public class NewBehaviourScript : MonoBehaviour {
    List<GameObject> NearGameobjects = new List<GameObject>();
    GameObject closetsObject;
    private float oldDistance = 9999;


    private void Something()
    {
        foreach (GameObject g in NearGameobjects)
        {
            float dist = Vector3.Distance(this.gameObject.transform.position, g.transform.position);
            if (dist < oldDistance)
            {
                closetsObject = g;
                oldDistance = dist;
            }
        }
    }

}

这篇关于查找并返回最近的带有标签的游戏​​对象,Unity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆