本文介绍了检测OnCollisionEnter2D以外的2D碰撞的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题,但是在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?

这是我最初想到的(在下面的代码中),但是由于某种奇怪的原因,我无法弄清楚,当平台将对象向上推时,变量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?

与Programmer讨论后,在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.

推荐答案

.他们很多!

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碰撞的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-16 03:21