Gin*_*der 3 c# collision-detection unity-game-engine raycasting
我的平台游戏的光线投射控制器的一个特点是玩家可以通过单向平台跳下来.这很好用,但我想添加额外的功能,如果玩家将跳跃的平台在一定距离内,玩家将只能跳下来.
我的平台控制器的跳跃功能如下所示:
我试过制作一个跳跃方法,从角色的盒子对撞机的中心创建一个新的光线投射(下面的代码中没有显示,因为到目前为止我完全不成功),并检查一定距离的碰撞.我的问题是它总是检测到它将要从中跳下的平台的对撞机,并且永远不会有机会检测到其他任何东西.
如何让这个新的光线投射忽略第一次碰撞,可能检测到光线距离内的碰撞,如果碰到某些东西让我的角色跳下来?另外,我是否需要进行新的光线投射,或者我可以在控制器上使用当前的光线投射?
这里有一些我的raycast控制器脚本,希望能够解释一些功能:
private void MoveVertically(ref Vector3 deltaMovement)
{
float directionY = Mathf.Sign(deltaMovement.y);
float rayLength = Mathf.Abs(deltaMovement.y) + skinWidth;
for (int i = 0; i < verticalRayCount; i++)
{
Vector2 rayOrigin = (directionY == -1) ? raycastOrigins.bottomLeft : raycastOrigins.topLeft;
rayOrigin += Vector2.right * (verticalRaySpacing * i + deltaMovement.x);
RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.up * directionY, rayLength, collisionMask);
Debug.DrawRay(rayOrigin, Vector2.up * directionY * rayLength, Color.red);
if (hit)
{
if (hit.collider.tag == "platformOneWay")
{
if (directionY == 1)
{
State.IsCollidingWithOneWay = true;
State.IsCollidingAbove = false;
continue;
}
if (CanJumpDown)
{
State.IsCollidingBelow = false;
continue;
}
}
deltaMovement.y = (hit.distance - skinWidth) * directionY;
rayLength = hit.distance;
Run Code Online (Sandbox Code Playgroud)
实际上,Unity3D提供了一个API Physics2D.RaycastAll,它不仅返回第一个光线投射命中,而且返回最大距离内的所有光线投射命中数组.
Physics2D.RaycastAll取两个Vector2参数(光线投射原点和方向)并返回一个数组RaycastHit2D,该数组按从原点到命中点的距离排序.
这里有一些代码可以显示这个函数的作用:
public class Raycaster : MonoBehaviour
{
private void Start()
{
RaycastHit2D[] hits = Physics2D.RaycastAll(transform.position, Vector2.down);
for (int i = 0; i < hits.Length; ++i)
{
Debug.LogFormat("The name of collider {0} is \"{1}\".",
i, hits[i].collider.gameObject.name);
}
}
}
Run Code Online (Sandbox Code Playgroud)