Esempio n. 1
0
    public void Move()
    {
        // compute move offset
        Vector2 moveOffset = m_velocity * Time.deltaTime;

        // integrate carrier velocity
        if (m_character.IsCarried())
        {
            m_carrierVel = m_character.GetCarrier().ComputeVelocityAt(transform.position);
            moveOffset  += m_carrierVel * Time.deltaTime;
        }

        // Initial position
        m_curPos = transform.position;

        // Clear detection variables
        m_posError   = Vector2.zero;
        m_touchHead  = false;
        m_touchFront = false;
        m_touchBack  = false;

        // refresh onewayground colliders
        m_oneWayColliders.RemoveAll(t => t.m_bOverlap == false); // remove non overlaping colliders
        m_oneWayColliders.ForEach(t => t.m_bOverlap = false);    // clear overlap flag for all colliders

        ClearRaysCollisionInfo(m_RaysGround);
        ClearRaysCollisionInfo(m_RaysUp);
        ClearRaysCollisionInfo(m_RaysFront);
        ClearRaysCollisionInfo(m_RaysBack);

        Vector2 v2InitialPos = m_curPos;

        // Move in 4 directions, constraint/correct movement if needed on each axis, prevent correction in opposite direction
        // NB : this method is not exact but this works for small displacements
        //      update this when we will have volume cast available with Box2D !
        //		Notice that order is important here, this should work in most 2d horizontal platformers
        bool bVerticalCorr = MoveVertical(ref moveOffset, false, true);
        bool bHorizCorr    = MoveHorizontal(ref moveOffset, true, true);

        MoveHorizontal(ref moveOffset, false, !bHorizCorr);
        MoveVertical(ref moveOffset, true, !bVerticalCorr);

        // move with physic engine so collisions with dynamic environment are valid
        if (Time.deltaTime > Mathf.Epsilon)
        {
            m_rigidBody.velocity = (m_curPos - v2InitialPos) / Time.deltaTime;

            // compute final velocity (position correction must not inject any velocity !)
            m_velocity = (m_curPos - v2InitialPos - m_posError) / Time.deltaTime;
        }
        else
        {
            m_rigidBody.velocity = Vector2.zero;
            m_velocity           = Vector2.zero;
        }

        // draw vel
        if (m_DrawRays)
        {
            Vector3 vel = m_rigidBody.velocity;
            Debug.DrawLine(transform.position, transform.position + vel, Color.green);
        }
    }