I'm having an issue with Unity2D collision detection. I am new to Unity and am following a tutorial to help set this up. I have set up the objects and layers correctly but I am having an issue with this code. I am using raycast with collision mask. I have tried both doing a bitmask conversion on the Layer but that hasn't seemed to work either. Does anyone have any ideas on what would be causing this issue? Here is my code:
void VerticalCollisions(ref Vector3 velocity) {
float directionY = Mathf.Sign (velocity.y);
float rayLength = Mathf.Abs (velocity.y) + skinWidth;
for (int i = 0; i < verticalRayCount; i ++) {
Vector2 rayOrigin = (directionY == -1)?raycastOrigins.bottomLeft:raycastOrigins.topLeft;
rayOrigin += Vector2.right * (verticalRaySpacing * i + velocity.x);
RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.up * directionY, rayLength, collisionMask);
Debug.DrawRay(rayOrigin, Vector2.up * directionY * rayLength,Color.red);
if (hit) {
velocity.y = (hit.distance - skinWidth) * directionY;
rayLength = hit.distance;
}
}
}
答案 0 :(得分:0)
您的代码有什么问题? 无论如何,你可以在没有编写任何代码的情况下使冲突工作。 我在Tiled制作了我的地图,最后填充了我想要与玩家碰撞的地图部分,将其转换为Unity 2D,在地图部分中获得了colissions标记,得到了颜色渲染然后创建了rigidbody2d。 干杯!