1 回答
TA贡献1876条经验 获得超5个赞
1)使用多个光线投射
在您的代码中,如果您的玩家的中心站在平台上方,游戏只会检测平台。要始终检测平台,您应该在角色对撞机的边界使用两个光线投射。
void Update()
{
// Cast the rays
castRays(transform.localScale.x / 2f);
}
private void castRays(float distanceFromCenter)
{
// Return if the ray on the left hit something
if(castRay(new Vector2(-distanceFromCenter, 0f) == true) { return; }
// Return if the ray on the right hit something
else if(castRay(new Vector2(distanceFromCenter, 0f) == true) { return; }
}
private bool castRay(Vector2 offset)
{
RaycastHit2D hit; // Stores the result of the raycast
// Cast the ray and store the result in hit
hit = Physics2D.Raycast(transform.position + offset, -Vector2.up, 2f, layerMask);
// If the ray hit a collider...
if(hit.collider != null)
{
// Destroy it
Destroy(hit.collider.gameObject);
// Return true
return true;
}
// Else, return false
return false;
}
可选:如果平台比玩家小或为了安全起见,您可以将射线重新包含在中心。
2)使用触发器
将 aBoxCollider2D放在角色的脚下并将“isTrigger”设置为 true。当它进入另一个碰撞器时,它将调用“OnTriggerEnter2D”。
void OnTriggerEnter2D(Collider2D other)
{
Destroy(other.gameObject);
}
- 1 回答
- 0 关注
- 234 浏览
添加回答
举报
