C#SphereCast如何使用hitInfo

C#SphereCast如何使用hitInfo,c#,unity3d,C#,Unity3d,我正在尝试制作一个fps游戏,让我的角色可以跳跃。 我正在使用spherecast来了解我的角色是否固定。 这就是我现在拥有的 public CapsuleCollider capsuleCollider; void Update() { if (IsGrounded() && Input.GetKeyDown(KeyCode.Space)) { rb.velocity = Vector3.up * jumpVelocity; }

我正在尝试制作一个fps游戏,让我的角色可以跳跃。 我正在使用spherecast来了解我的角色是否固定。 这就是我现在拥有的

    public CapsuleCollider capsuleCollider;
void Update()
{
    if (IsGrounded() && Input.GetKeyDown(KeyCode.Space))
    {
        rb.velocity = Vector3.up * jumpVelocity;
    }
}
private bool IsGrounded()
{
    float downCheck = capsuleCollider.bounds.extents.y + 0.1f;
    return Physics.SphereCast(capsuleCollider.bounds.center, capsuleCollider.bounds.size.x, Vector3.down, hit, capsuleCollider.bounds.extents.y + extraHeight);
}
我不知道我和hit有什么关系。 有人能帮我吗

不知道我和hit有什么关系

你想要什么都行;)

请参见
hit
是一个参数,这意味着它的值将在
physical.SphereCast
方法中分配

你现在可以像这样做

private bool IsGrounded(out RayCastHit hit)
{
    var downCheck = capsuleCollider.bounds.extents.y + 0.1f;
    return Physics.SphereCast(capsuleCollider.bounds.center, capsuleCollider.bounds.size.x, Vector3.down, out hit, capsuleCollider.bounds.extents.y + extraHeight);
}
或者您似乎无论如何都不需要它,所以您可以传入
out var hit
并进一步忽略它,或者-在c中实际上存在忽略
out
调用的参数的东西-您可以简单地传入
out
如下

private bool IsGrounded()
{
    float downCheck = capsuleCollider.bounds.extents.y + 0.1f;
    return Physics.SphereCast(capsuleCollider.bounds.center, capsuleCollider.bounds.size.x, Vector3.down, out _, capsuleCollider.bounds.extents.y + extraHeight);
}
这会告诉编译器您不使用该值,因此您甚至不必关心它的类型或名称


另请注意:您确定要执行
SphereCast
?这意味着你射出一个球体,看看它在某个方向上会击中什么。。。你更愿意使用例如a