C# 有没有办法使二维碰撞器忽略另一个二维碰撞器?
我目前正在承担一项大学作业,其中我必须创建一个2D游戏。我选择了一个“绑定艾萨克”风格的地下城爬虫。这个问题开始于在我的房间精灵中添加框碰撞器。地下城的生成是由每个游戏对象上的繁殖点创建的。有一个封闭的房间,当两个产卵点在一个房间产卵之前发生碰撞和破坏时,该房间产卵 随着对房间添加碰撞器,繁殖点将与房间发生碰撞并将其摧毁。由于驱逐舰的存在,这种情况尤其发生在进入室,因为繁殖室通常有4个繁殖点在彼此的顶部生成-因此驱逐舰会移除这些点 下面是我的地下城世代课程 房间产卵器:C# 有没有办法使二维碰撞器忽略另一个二维碰撞器?,c#,unity3d,collider,C#,Unity3d,Collider,我目前正在承担一项大学作业,其中我必须创建一个2D游戏。我选择了一个“绑定艾萨克”风格的地下城爬虫。这个问题开始于在我的房间精灵中添加框碰撞器。地下城的生成是由每个游戏对象上的繁殖点创建的。有一个封闭的房间,当两个产卵点在一个房间产卵之前发生碰撞和破坏时,该房间产卵 随着对房间添加碰撞器,繁殖点将与房间发生碰撞并将其摧毁。由于驱逐舰的存在,这种情况尤其发生在进入室,因为繁殖室通常有4个繁殖点在彼此的顶部生成-因此驱逐舰会移除这些点 下面是我的地下城世代课程 房间产卵器: using System
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;
}
}
}
使用System.Collections.Generic;
使用UnityEngine;
公共类RoomSpawner:MonoBehavior
{
公共int开放方向;
//1=需要底部门
//2=需要顶门
//3=需要左车门
//4=需要右门
//因此,对于右边有门的房间,您将键入3,因为隔壁房间需要一个左边的门来连接两个房间。
私人房间模板;
私人国际兰特;
private bool sprowned=false;
私人向量机3入口位置;
void Start()
{
templates=GameObject.FindGameObjectWithTag(“房间”).GetComponent();
调用(“生成”,0.1f);
}
void Spawn()
{
如果(生成==false)
{
rand=Random.Range(0,templates.bottomRooms.Length);
如果(打开方向==1)
{
//需要带底门的产卵室
实例化(templates.bottomRooms[rand],transform.position,templates.bottomRooms[rand].transform.rotation);
}
否则如果(打开方向==2)
{
//需要带顶门的产卵室
实例化(templates.topRooms[rand]、transform.position、templates.topRooms[rand]、transform.rotation);
}
否则如果(打开方向==3)
{
//需要带左门的产卵室
实例化(templates.leftRooms[rand]、transform.position、templates.leftRooms[rand]、transform.rotation);
}
否则如果(打开方向==4)
{
//需要右门产卵室
实例化(templates.rightRooms[rand]、transform.position、templates.rightRooms[rand]、transform.rotation);
}
繁殖=真;
}
}
无效OnTiggerEnter2D(碰撞的R2D其他)
{
entryPos=templates.entryRoom.transform.position;
if(其他.CompareTag(“繁殖点”))
{
if(other.GetComponent().spawned==false&&spawned==false&&transform.position!=entryPos)
{
实例化(templates.closedRoom、transform.position、Quaternion.identity);
}
繁殖=真;
}
}
}
文件室模板:
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.
}
}
}
使用System.Collections.Generic;
使用UnityEngine;
公共类RoomTemplates:MonoBehavior
{
公共游戏对象[]底层房间;
公共游戏对象[]顶层房间;
公共游戏对象[]左室;
公共游戏对象[]右室;
公共游戏对象入口;
公共游戏对象关闭;
公共名单室;
公共浮动等待时间;
私生子老板;
公共游戏对象老板;
私有void更新()
{
如果(waitTimeLayerMask可能是您要查找的对象。您可以将不希望碰撞的游戏对象保留在不同的层中,并更改碰撞矩阵(项目设置>物理2D>层碰撞矩阵)因此,这些对象不会相互碰撞。LayerMask可能就是您要查找的对象。您可以将不希望碰撞的游戏对象保留在不同的层中,并更改碰撞矩阵(ProjectSettings>Physics2D>Layer collision matrix)因此,这些对象不会相互碰撞。谢谢你的建议!但是,我在提问之前确实尝试过这个方法。它导致了封闭的房间不断地在入口房间上繁殖,与入口广场一样被删除。谢谢你的建议!但是,我在提问之前尝试过这个方法。它导致了c丢失的房间一直在入口房间上繁殖,与被删除的入口广场相对应。
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>());
}
}
}