コード例 #1
0
    /// <summary>
    /// This method retuns a vector of  direction that was gien and given the speed it returns a vetor to move in that direction
    ///  and shortens it if it will collide with someting in the given distance
    /// </summary>
    /// <param name="rayCount">This is the number of rays that will ge cast to check for collision</param>
    /// <param name="direction">This is the direction of where you want the raycast</param>
    /// <param name="bounds">This is the bound of your collider</param>
    /// <param name="vs">This is the vertical speed, to move with</param>
    /// <param name="hs">This is the horizontal speed to move with</param>
    /// <param name="tf">This is the transform of the object</param>
    /// <param name="useDeltaTime">If the ditance should be miltuplied by deltaTime</param>
    /// <returns>Returns a vector of the max movement vector (gievn deltaTime) to translate to</returns>
    private Vector3 CalculateMoveVectorCollision(int rayCount, RayDirectionSearch direction, Bounds bounds, float vs, float hs, Transform tf, bool useDeltaTime = true)
    {
        float   distance;
        Vector3 rayDirection, rayJump, startPos;

        CalculateNecesaryValues(rayCount, direction, bounds, vs, hs, tf, useDeltaTime, out distance, out rayDirection, out rayJump, out startPos);
        Vector3      moveVector;
        RaycastHit2D tmp;

        DoRaycast(rayCount, direction, distance, rayDirection, rayJump, startPos, out moveVector, out tmp);
        if (tmp.collider != null)
        {
            OnCollision(tmp);
        }
        return(moveVector);
    }
コード例 #2
0
    private RaycastHit2D DoRaycast(int rayCount, RayDirectionSearch direction, float distance, Vector3 rayDirection, Vector3 rayJump, Vector3 startPos, out Vector3 moveVector, out RaycastHit2D tmp, string searchTag = null)
    {
        RaycastHit2D rch = new RaycastHit2D();

        moveVector = rayDirection * distance;
        float minDistance = Mathf.Infinity;

        tmp = new RaycastHit2D();
        for (int i = 0; i < rayCount; i++)
        {
            Debug.DrawRay(startPos, rayDirection * distance, Color.green);
            RaycastHit2D hit = Physics2D.Raycast(startPos, rayDirection, distance, collisionMask);
            if (hit.collider != null)
            {
                if (searchTag != null)
                {
                    if (hit.collider.tag.Equals(searchTag))
                    {
                        Debug.Log("The tag of the hit target and the tag of the earch: " + hit.collider.tag + " : " + searchTag);
                        rch = hit;
                    }
                }
                if (direction == RayDirectionSearch.DOWN || direction == RayDirectionSearch.UP)
                {
                    if (minDistance > hit.distance)
                    {
                        minDistance  = hit.distance;
                        moveVector.y = hit.distance * rayDirection.y;
                        tmp          = hit;
                    }
                }
                else
                {
                    if (minDistance > hit.distance)
                    {
                        minDistance  = hit.distance;
                        moveVector.x = hit.distance * rayDirection.x;
                    }
                }
            }
            startPos += rayJump;
        }
        return(rch);
    }
コード例 #3
0
    private static void CalculateNecesaryValues(int rayCount, RayDirectionSearch direction, Bounds bounds, float vs, float hs, Transform tf, bool useDeltaTime, out float distance, out Vector3 rayDirection, out Vector3 rayJump, out Vector3 startPos)
    {
        float   jumpLength;
        Vector3 startPosOffset = Vector3.zero;
        float   percentOffset  = 0.02f;

        // calculate the jumpdistance dpending on the number of rays and direktion
        if (direction == RayDirectionSearch.DOWN || direction == RayDirectionSearch.UP)
        {
            jumpLength = (float)(bounds.extents.x * 2 * (1 - percentOffset)) / (rayCount - 1);
            if (useDeltaTime)
            {
                distance = vs * Time.deltaTime;
            }
            else
            {
                distance = vs;
            }
            startPosOffset.x = bounds.extents.x * 2 * (percentOffset / 2);
        }
        else
        {
            jumpLength = (float)(bounds.extents.y * 2 * (1 - percentOffset)) / (rayCount - 1);
            if (useDeltaTime)
            {
                distance = hs * Time.deltaTime;
            }
            else
            {
                distance = hs;
            }
            startPosOffset.y = bounds.extents.y * 2 * (percentOffset / 2);
        }
        rayJump = Vector3.zero;
        // setup the ray search direktion, a vector for jumping and start position depending on the
        // search direktion
        switch (direction)
        {
        case RayDirectionSearch.UP:
            rayDirection = tf.up;
            rayJump.x    = jumpLength;
            startPos     = new Vector3(bounds.min.x, bounds.max.y, 0) + startPosOffset;
            break;

        case RayDirectionSearch.DOWN:
            rayDirection = tf.up * (-1);
            rayJump.x    = jumpLength;
            startPos     = new Vector3(bounds.min.x, bounds.min.y, 0) + startPosOffset;
            break;

        case RayDirectionSearch.LEFT:
            rayDirection = tf.right * (-1);
            rayJump.y    = jumpLength * (-1);
            startPos     = new Vector3(bounds.min.x, bounds.max.y, 0) - startPosOffset;
            break;

        default:
            rayDirection = tf.right;
            rayJump.y    = jumpLength * (-1);
            startPos     = new Vector3(bounds.max.x, bounds.max.y, 0) - startPosOffset;
            break;
        }
    }