有什么方法可以让一个2D对撞机忽略另一个2D对撞机?

问题描述 投票:0回答:1

我目前正在进行一项大学作业,我必须创建一个2D游戏。我选择了 "以撒的束缚 "式的地牢爬行游戏。这个问题是从我的房间精灵中加入盒子碰撞器开始的。地牢的生成是由每个游戏对象上的产卵点创建的。有一个封闭的房间,当两个产卵点碰撞并破坏后才会产生一个房间。

在房间中加入了碰撞器后,产卵点与房间发生碰撞并破坏。这种情况专门发生在入口房间,因为毁灭者位于那里,因为产卵房间经常有4个产卵点被生成在一起--所以毁灭者会移除这些产卵点。

以下是我的地下城生成类。

房间生成器。

using System.Collections.Generic;
using UnityEngine;

public class RoomSpawner : MonoBehaviour
{
    public int openingDirection;
    //1 = need bottom door
    //2 = need top door
    //3 = need left door
    //4 = need right door
    //So for a room with a door on the right, you will type 3, as a left door is needed in the next room to connect the two rooms. 

    private RoomTemplates templates;
    private int rand;
    private bool spawned = false;
    private Vector3 entryPos;

    void Start()
    {

        templates = GameObject.FindGameObjectWithTag("Rooms").GetComponent<RoomTemplates>();
        Invoke("Spawn", 0.1f);
    }

    void Spawn()
    {
        if (spawned == false)
        {
            rand = Random.Range(0, templates.bottomRooms.Length);
            if (openingDirection == 1)
            {
                //Need to spawn room with BOTTOM door
                Instantiate(templates.bottomRooms[rand], transform.position, templates.bottomRooms[rand].transform.rotation);
            }
            else if (openingDirection == 2)
            {
                //Need to spawn room with TOP door
                Instantiate(templates.topRooms[rand], transform.position, templates.topRooms[rand].transform.rotation);
            }
            else if (openingDirection == 3)
            {
                //Need to spawn room with LEFT door
                Instantiate(templates.leftRooms[rand], transform.position, templates.leftRooms[rand].transform.rotation);
            }
            else if (openingDirection == 4)
            {
                //Need to spawn room with RIGHT door
                Instantiate(templates.rightRooms[rand], transform.position, templates.rightRooms[rand].transform.rotation);
            }
            spawned = true;
        }

    }

    void OnTriggerEnter2D(Collider2D other)
    {
        entryPos = templates.entryRoom.transform.position;
        if (other.CompareTag("Spawn Point"))
        {
            if (other.GetComponent<RoomSpawner>().spawned == false && spawned == false && transform.position != entryPos)
            {
                Instantiate(templates.closedRoom, transform.position, Quaternion.identity);


            }
            spawned = true;
        }
    }
}

房间模板:

using System.Collections.Generic;
using UnityEngine;

public class RoomTemplates : MonoBehaviour
{
    public GameObject[] bottomRooms;
    public GameObject[] topRooms;
    public GameObject[] leftRooms;
    public GameObject[] rightRooms;

    public GameObject entryRoom;
    public GameObject closedRoom;

    public List<GameObject> rooms;

    public float waitTime;
    private bool spawnedBoss;
    public GameObject boss;

    private void Update()
    {
        if(waitTime <= 0 && spawnedBoss ==false) //if the wait time is 0 and boss isnt spawned a boss will spawn.
        {
            for (int i = 0; i < rooms.Count; i++)// will run as long as i is less than the no. of elements in list
            {
                if (i == rooms.Count - 1)//lists start with an index of zero
                {
                    Instantiate(boss, rooms[i].transform.position, Quaternion.identity);//spawn boss at the room of index i's position
                    spawnedBoss = true; //stop bosses infinitly spawning
                }
            }
        } else
        {
            waitTime -= Time.deltaTime; //slowly decrease wait time value
            //we must wait before spawning boss to ensure all rooms have been spawned.
        }

    }
}

添加房间

using System.Collections.Generic;
using UnityEngine;

public class AddRoom : MonoBehaviour
{
    private RoomTemplates templates;
    void Start()
    {
        templates = GameObject.FindGameObjectWithTag("Rooms").GetComponent<RoomTemplates>();
        templates.rooms.Add(this.gameObject);
    }
}


毁灭者

using System.Collections.Generic;
using UnityEngine;

public class Destroyer : MonoBehaviour
{
    private RoomTemplates templates;
    void OnTriggerEnter2D(Collider2D other)
    {
        if(gameObject.tag == "Spawn Point")
        {
            Destroy(other.gameObject);
        } else if(gameObject.tag != "Spawn Point")
        {
            Physics2D.IgnoreCollision(templates.entryRoom.GetComponent<Collider2D>(), GetComponent<Collider2D>());
        }


    }
}
c# unity3d collider
1个回答
2
投票

图层蒙皮(LayerMask) 可能就是你要找的那个。你可以将不想碰撞的游戏对象放在不同的层中,并改变碰撞矩阵(ProjectSettings > Physics2D > Layer Collision Matrix),这样这些对象就不会相互碰撞了。

© www.soinside.com 2019 - 2024. All rights reserved.