检测除 OnCollisionEnter2D 之外的 2D 碰撞的方法 [英] Ways to detect 2D collision other than OnCollisionEnter2D
问题描述
这可能是一个愚蠢的问题,但在 if 语句中,是否可以检查对象当前是否与另一个对象发生碰撞?
This might come off as a stupid question, but in an if statement, is it possible to check if the object is currently colliding with another object?
这是我最初想到的(在下面的代码 snipit bellow 中),但由于一些我无法弄清楚的奇怪原因,当对象被平台向上推时,变量 onFloor 有时为假.>
Here is what I had thought of initially (in the code snipit bellow) but for some odd reason that I can't figure out, the variable onFloor is sometimes false when the object is being pushed upwards by the Platform.
void OnCollisionEnter2D(Collision2D c)
{
switch(c.gameObject.tag)
{
case "Platform":
onFloor = true;
break;
default:
break;
}
}
void OnCollisionExit2D(Collision2D c)
{
switch(c.gameObject.tag)
{
case "Platform":
onFloor = false;
break;
default:
break;
}
}
出于这个原因,我问是否有任何方法可以检测在代码中所述对象的圆形对撞机是否与相交对象的框对撞机发生碰撞.类似的东西
And for that reason, I am asking if there are any ways to detect if the circle collider of said object is colliding with box collider of intersecting object while in code. something like
if(CircleCollider2D.CollidingObject != null && CircleCollider2D.CollidingObject.tag == "Platform")
{ /*Code I'd like to do here*/ }
现在这只是我的想象,试图想出一些可行的方法,但你明白了.
Now that's only my imagination trying to think of some way of it that could work but you get the point.
那么,我问,有没有办法满足我的想象?
So, I ask, are there any solutions for my imagination?
与程序员讨论后,IsTouching 被加下划线并给出错误:'Collider2D' 不包含'IsTouching' 的定义并且没有扩展方法'IsTouching' 接受'Collider2D' 类型的第一个参数可以找到(你是缺少 using 指令或程序集引用?).
After discussing with Programmer, IsTouching is underlined and giving the error: 'Collider2D' does not contain a definition for 'IsTouching' and no extension method 'IsTouching' accepting a first argument of type 'Collider2D' could be found (are you missing a using directive or an assembly reference?).
这是精简后的代码:
using UnityEngine;
public class Ball : MonoBehaviour
{
Vector2 jumpVelocity;
public Collision2D platform;
// Use this for initialization
void Start()
{
jumpVelocity = new Vector2(0, rigidbody2D.gravityScale * 100);
}
void FixedUpdate()
{
if (Input.GetKey(KeyCode.Space) && collider2D.IsTouching(platform))
{
rigidbody2D.AddForce(jumpVelocity, ForceMode2D.Force);
}
}
}
请注意,即使将 collider2D 更改为 Collision2D 变量并采用该变量.collider.IsTouching 也会导致相同的错误.
note that even changing collider2D to a Collision2D variable and taking that variable.collider.IsTouching results the same error.
推荐答案
是否有与
OnCollisionEnter2D
是的.很多!
CircleCollider2D
,其他 2D 碰撞器及其基类 Collision2D
,已内置函数来执行此操作.
CircleCollider2D
, other 2D colliders and its base class Collision2D
, have built in functions to do this.
public bool IsTouching(Collider2D collider); //Check collision by the provided collider
public bool IsTouchingLayers(); //Check collision by any collision
public bool IsTouchingLayers(int layerMask); //Check collision by layer
public bool OverlapPoint(Vector2 point); //Check collision by position
第一个函数更适合于此.
The first function is more appropriate for this.
简单示例:
Collision2D collider1;
Collision2D collider2;
void Update()
{
//Checks if collider1 is touching with collider2
if (collider1.collider.IsTouching(collider2.collider))
{
}
}
OnCollisionEnter2D
函数示例:
public CircleCollider2D myCircleCollider;
void OnCollisionEnter2D(Collision2D c)
{
if (c.collider.IsTouching(myCircleCollider))
{
}
}
这篇关于检测除 OnCollisionEnter2D 之外的 2D 碰撞的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!