Example #1
0
    // Use this for initialization
    void Start()
    {
        m_collider = GetComponent <BoxCollider2D> ();

        m_size.x = m_collider.bounds.size.x;
        m_size.y = m_collider.bounds.size.y;
        m_offset = m_collider.bounds.center - transform.position;

        m_oneWay = false;
        CollisionCreator collisionCreator = GetComponent <CollisionCreator> ();

        if (collisionCreator != null)
        {
            m_oneWay = collisionCreator.isOneway;
        }

        // Remove children
        foreach (Transform location in m_locations)
        {
            if (location.parent == transform)
            {
                location.SetParent(null);
            }
        }
    }
Example #2
0
    void FixedUpdate()
    {
        DrawDebug();

        float frameTime = Time.deltaTime;

        if (frameTime <= 0)
        {
            return;
        }

        m_position = transform.position;

        Vector2 collisionPos = m_position + m_offset;
        Vector2 localVelocity;

        m_onGround = false;
        if (m_gravityScale > 0)
        {
            m_velocity.y -= s_gravity * m_gravityMultiplier * m_gravityScale * frameTime;
            localVelocity = new Vector2(0, m_velocity.y * frameTime);

            // Trace floor
            if (localVelocity.y < 0)
            {
                RaycastHit2D hit2D = Physics2D.BoxCast(collisionPos, m_size, 0, localVelocity, -localVelocity.y);
                if (hit2D)
                {
                    m_position  += localVelocity * hit2D.fraction + (hit2D.normal * 0.01f);
                    m_velocity.y = 0;
                    m_onGround   = true;

                    collisionPos.y = m_position.y + m_offset.y;

                    if (hit2D.transform.GetComponent <MovingPlatform>() != null)
                    {
                        transform.SetParent(hit2D.transform);
                    }
                    else
                    {
                        transform.SetParent(null);
                    }
                }
            }

            if (!m_onGround)
            {
                transform.SetParent(null);
            }
        }

        // Trace walking
        m_hitWall     = false;
        m_hitCeiling  = false;
        localVelocity = m_velocity * frameTime;

        for (uint resolveCount = 0; resolveCount < 3; resolveCount++)
        {
            float magnitude = localVelocity.magnitude;
            if (magnitude > 0.0f)
            {
                RaycastHit2D hit2D = Physics2D.BoxCast(collisionPos, m_size, 0, localVelocity, magnitude);

                bool collide = false;
                if (hit2D)
                {
                    CollisionCreator collision = hit2D.transform.GetComponent <CollisionCreator>();
                    if (collision && collision.isOneway)
                    {
                        collide = false;
                    }
                    else
                    {
                        collide = !Physics2D.GetIgnoreCollision(hit2D.collider, m_collider);
                    }
                }

                if (collide)
                {
                    if (Mathf.Abs(hit2D.normal.x) > m_WallNormal)
                    {
                        m_hitWall = true;
                    }
                    else
                    {
                        // Debug.Log (hit2D.transform.name + ":" + hit2D.transform.position);

                        m_hitCeiling = true;
                    }
                    m_wallDirection = Mathf.Sign(hit2D.normal.x);

                    /*
                     * if(m_hitWall) {
                     *      localVelocity.x = 0;
                     * }
                     * m_position += (localVelocity * hit2D.fraction);// + (hit2D.normal * 0.01f);
                     *
                     * collisionPos = m_position + m_offset;
                     * localVelocity =  -(1.0f * Vector2.Dot(hit2D.normal, localVelocity) * hit2D.normal) + localVelocity;
                     * // localVelocity *= 1.0f - hit2D.fraction;
                     */
                    collisionPos = m_position + (localVelocity * hit2D.fraction);

                    // Hack - would be nice to remove this
                    if (m_hitWall)
                    {
                        m_position.x  += (localVelocity.x * hit2D.fraction) + (hit2D.normal.x * 0.01f);
                        collisionPos.y = m_position.y;
                    }
                    else
                    {
                        collisionPos.x = m_position.x;
                    }

                    collisionPos += m_offset;
                    localVelocity = -(1.0f * Vector2.Dot(hit2D.normal, localVelocity) * hit2D.normal) + localVelocity;
                }
                else
                {
                    m_position += localVelocity;
                    break;
                }
            }
        }
        m_velocity         = localVelocity / frameTime; // Change this to distance travelled then we can modify the velocity internally
        transform.position = m_position;

        if (m_showDebug)
        {
            Debug.Log(Time.frameCount +
                      " :" + transform.name +
                      " :" + " OnGround:" + m_onGround +
                      " HitWall:" + m_hitWall +
                      " Ground=" + ((transform.parent == null) ? "null" : transform.parent.name));
        }
    }