Joh*_*ohn 1 c# collision-detection game-engine unity-game-engine game-physics
我的游戏中有一个特定的对象,我正在尝试查看该对象是否触发了多个触发器.我尝试使用下面的代码但由于某种原因它不起作用.
void OnTriggerEnter2D(Collider2D col)
{
if (col.tag == "speed")
{
//do something
}
else if (col.tag == "speed" && col.tag == "point")
{
//do something
}
}
Run Code Online (Sandbox Code Playgroud)
如何识别对象是否仅命中"Col1"或"Col1"和"Col2"
OnTriggerEnter仅在对象与一个特定触发器发生碰撞时调用.因此,对撞机的(标记col)不能speed和point在同一时间.
您必须使用布尔变量跟踪对象是否与您的触发器发生冲突,例如:
private bool collidingWithSpeed;
private bool collidingWithPoint;
void OnTriggerEnter2D(Collider2D col)
{
if (col.CompareTag("speed"))
{
collidingWithSpeed = true ;
//do something
}
else if (col.CompareTag("point"))
{
collidingWithPoint = true ;
//do something
}
if( collidingWithSpeed && collidingWithPoint )
{
// Do something when your object collided with both triggers
}
}
// Don't forget to set the variables to false when your object exits the triggers!
void OnTriggerExit2D(Collider2D col)
{
if (col.CompareTag("speed"))
{
collidingWithSpeed = false;
}
else if (col.CompareTag("point"))
{
collidingWithPoint = false;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2754 次 |
| 最近记录: |