コード例 #1
0
    //This takes the results of a sidecast and tries to determine if a wall is found.
    //Using the sidecast information, it sends out three probes (from the center of the top and bottom hemispheres and the center of the capsule).
    //It counts the amount of hits that are valid and stores this for other classes to access.
    void WallCast(RaycastHit a_SidecastHit)
    {
        if (Vector3.Angle(Vector3.up, a_SidecastHit.normal) > m_CapsuleCollider.GetMaxWallAngle() || Vector3.Angle(Vector3.up, a_SidecastHit.normal) < m_CapsuleCollider.GetMaxGroundedAngle())
        {
            m_WallCastCount = 0;
            return;
        }
        float   wallCastMargin   = m_CapsuleCollider.GetWallCastMargin();
        float   wallCastDistance = m_CapsuleCollider.GetWallCastDistance();
        Vector3 hitPos           = a_SidecastHit.point;
        Vector3 normal           = a_SidecastHit.normal;
        Vector3 currentUp        = m_CapsuleCollider.GetUpDirection();
        Vector3 direction        = CState.GetDirectionAlongNormal(Vector3.up, normal);
        Vector3 startHitPos1     = Vector3.zero;
        Vector3 startHitPos2     = Vector3.zero;
        Vector3 startHitPos3     = Vector3.zero;
        Vector3 normalOff        = normal * (m_CapsuleCollider.GetRadius() - wallCastMargin);

        //Determines where the sidecast has hit. This to orient the probes in the correct way.
        if (Vector3.Dot(currentUp, hitPos) <= Vector3.Dot(currentUp, m_CapsuleCollider.GetDownCenter()))//Hit on the lower hemisphere
        {
            startHitPos1 = m_CapsuleCollider.GetDownCenter() + direction * m_CapsuleCollider.GetDefaultLength() - normalOff;
            startHitPos2 = m_CapsuleCollider.GetDownCenter() - normalOff;
            startHitPos3 = m_CapsuleCollider.GetDownCenter() + direction * m_CapsuleCollider.GetDefaultLength() * 0.5f - normalOff;
        }
        else if (Vector3.Dot(currentUp, hitPos) >= Vector3.Dot(currentUp, m_CapsuleCollider.GetUpCenter()))//Hit on the upper hemisphere
        {
            startHitPos1 = m_CapsuleCollider.GetUpCenter() - direction * m_CapsuleCollider.GetDefaultLength() - normalOff;
            startHitPos2 = m_CapsuleCollider.GetUpCenter() - normalOff;
            startHitPos3 = m_CapsuleCollider.GetUpCenter() - direction * m_CapsuleCollider.GetDefaultLength() * 0.5f - normalOff;
        }
        else //Hit somewhere in the middle
        {
            startHitPos1 = m_CapsuleCollider.GetDownCenter(true) + direction * m_CapsuleCollider.GetDefaultLength() - normalOff;
            startHitPos2 = m_CapsuleCollider.GetDownCenter(true) - normalOff;
            startHitPos3 = m_CapsuleCollider.GetDownCenter(true) + direction * m_CapsuleCollider.GetDefaultLength() * 0.5f - normalOff;
        }

        RaycastHit newHit;

        m_WallCastCount = 0;
        if (Physics.Raycast(startHitPos1, -normal, out newHit, wallCastDistance + wallCastMargin, m_CapsuleCollider.GetLayerMask()))
        {
            m_WallCastCount++;
        }
        if (Physics.Raycast(startHitPos2, -normal, out newHit, wallCastDistance + wallCastMargin, m_CapsuleCollider.GetLayerMask()))
        {
            m_WallCastCount++;
        }
        if (Physics.Raycast(startHitPos3, -normal, out newHit, wallCastDistance + wallCastMargin, m_CapsuleCollider.GetLayerMask()))
        {
            m_WallCastCount++;
        }
    }
コード例 #2
0
ファイル: CapsuleTransform.cs プロジェクト: AdlanSADOU/EPIJAM
    public Vector3 GetUpCenter(bool a_UseOriginalLength = false)
    {
        if (a_UseOriginalLength && m_Length != m_CapsuleCollider.GetDefaultLength())
        {
            switch (m_LastResizeMethod)
            {
            case CapsuleResizeMethod.FromBottom:
                if (m_CapsuleCollider.CanBeResized(m_CapsuleCollider.GetDefaultLength(), CapsuleResizeMethod.FromBottom))
                {
                    return(GetDownCenter() + m_UpDirection * m_CapsuleCollider.GetDefaultLength());
                }
                break;

            case CapsuleResizeMethod.FromCenter:
                return(m_Position + m_UpDirection * m_CapsuleCollider.GetDefaultLength() * 0.5f);
            }
        }
        return(m_Position + m_UpDirection * m_Length * 0.5f);
    }