【发布时间】:2015-08-08 01:28:38
【问题描述】:
我不知道我为什么会得到它。只有当光线投射到物体上时才会弹出。
void Update(){
RaycastHit2D hitSideRight = new RaycastHit2D();
hitSideRight = Physics2D.Raycast (transform.position, Vector2.right, 1.2f, 1 << LayerMask.NameToLayer("Ground"));
Debug.DrawRay (transform.position, Vector2.right* 1.2f);
RaycastHit2D hitSideLeft = new RaycastHit2D();
hitSideLeft = Physics2D.Raycast (transform.position, -Vector2.right, 1.2f, 1 << LayerMask.NameToLayer("Ground"));
Debug.DrawRay (transform.position, -Vector2.right* 1.2f);
if(hitSideLeft.collider != null || hitSideRight.collider != null){
CancelMove();
Debug.Log(hitSideRight.collider.tag.ToString());
}
}
我收到这行代码的错误(仅当射线与对象接触时):Debug.Log(hitSideRight.collider.tag.ToString());
【问题讨论】:
-
当 NRE 实际与左侧碰撞(使您的条件为真)而不是右侧时,您会得到 NRE?并且没有必要在那里
new你的两个 RaycastHit2D。 -
但如果我不使用 new 而不是一个简单的指针,不是吗?
标签: unity3d raycasting