首页 > 解决方案 > Unity2D 碰撞,if 语句

问题描述

我有一点 unityCode 来检查角色是否接地

private void FixedUpdate(){
    is_on_ground = false;

    // The player is grounded if a circlecast to the groundcheck position hits anything designated as ground
    // This can be done using layers instead but Sample Assets will not overwrite your project settings.
    Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
    for (int i = 0; i < colliders.Length; i++) {
        if (colliders[i].gameObject != gameObject) {
            is_on_ground = true;
        }
    }
}

有人可以解释 if 语句的if (colliders[i].gameObject != gameObject)作用吗?

标签: c#unity3dif-statementcollision-detection

解决方案


它正在检查以确保在 Overlap 中找到的当前对撞机不是脚本所在的游戏对象。

这很奇怪,因为它是对特定层的接地测试,因此接地的对象很可能不属于地面。


推荐阅读