防止统一产生的游戏对象重叠 [英] Prevent overlapping of spawned game objects in unity

查看:100
本文介绍了防止统一产生的游戏对象重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图统一制作一个简单的2D平台游戏.我设法移动了播放器和所有其他东西.我现在面临的问题是随机生成到游戏中的预制件重叠.所以我的问题是如何防止游戏对象重叠.我使用的预制件具有不同的尺寸(长度). 这是我使用的C#代码:

Iam trying to make a simple 2d platformer game in unity. I managed to move the player and all other stuffs. The problem now i am facing is that the prefabs that randomly spawned onto the game is overlapping. So my question is how to prevent the overlapping of game objects. The prefabs i used is having different dimension(length). Here is the c# code i used:

    public class spawnscript : MonoBehaviour {
    public GameObject[] obj;
    public float spawnMin;
    public float spawnMax;

    // Use this for initialization
    void Start () {
        Spawn ();
    }

    void Spawn()
    {
        Instantiate (obj [Random.Range (0, obj.GetLength (0))], transform.position, Quaternion.identity);
        Invoke ("Spawn", Random.Range (spawnMin, spawnMax));
    }
}

推荐答案

void Spawn() {
    Instantiate(obj [Random.Range (0, obj.GetLength (0))], transform.position +(ADD OFFSET HERE WITH PREVIOUS OBJECTS Position), Quaternion.identity);
    Invoke("Spawn", Random.Range (spawnMin, spawnMax));
}

这篇关于防止统一产生的游戏对象重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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