Example #1
0
    private void applyGravity()
    {
        if (grounded)
        {
            velocity.y = 0;
        }
        velocity.y += Physics2D.gravity.y * Time.deltaTime;
        grounded    = false;
        Collider2D[] hits    = Physics2D.OverlapBoxAll(transform.position, boxCollider.size, 0);
        bool         noLever = true;

        foreach (Collider2D hit in hits)
        {
            if (hit == boxCollider)
            {
                continue;
            }
            if (hit.gameObject.tag == "Lever")
            {
                noLever = false;
                Lever lever = hit.gameObject.GetComponent <Lever>();
                Debug.Log("Lever Hit! Im BoxMover");
                lever.collisionWithCharacterOccurred();
                continue;
            }
            ColliderDistance2D colliderDistance = hit.Distance(boxCollider);
            if (colliderDistance.isOverlapped)
            {
                transform.Translate(colliderDistance.pointA - colliderDistance.pointB);
                if (Vector2.Angle(colliderDistance.normal, Vector2.up) < 90 && velocity.y < 0)
                {
                    grounded = true;
                }
            }
        }
        if (noLever && lastLever != null)
        {
            lastLever.unpressLever();
            lastLever = null;
        }
        transform.Translate(velocity * Time.deltaTime);
    }
Example #2
0
    private void Update()
    {
        updateTouch();
        if (grounded)
        {
            velocity.y = 0;
            if (jumpInput != 0)
            {
                velocity.y = Mathf.Sqrt(2 * jumpHeight * Mathf.Abs(Physics2D.gravity.y));
            }
        }
        float acceleration = grounded ? walkAcceleration : airAcceleration;
        float deceleration = grounded ? groundDeceleration : 0;

        if (moveInput != 0)
        {
            velocity.x = Mathf.MoveTowards(velocity.x, speed * moveInput, acceleration * Time.deltaTime);
        }
        else
        {
            if (!onPlatform)
            {
                velocity.x = Mathf.MoveTowards(velocity.x, 0, deceleration * Time.deltaTime);
            }
        }
        velocity.y += Physics2D.gravity.y * Time.deltaTime;
        grounded    = false;
        // Retrieve all colliders we have intersected after velocity has been applied.
        Collider2D[] hits = Physics2D.OverlapBoxAll(transform.position, boxCollider.size, 0);
        onPlatform = false;
        bool noBox   = true;
        bool noLever = true;

        foreach (Collider2D hit in hits)
        {
            // Ignore our own collider.
            if (hit == boxCollider)
            {
                continue;
            }
            if (hit.gameObject.tag == "Deadzone")
            {
                transform.position = startPos;
                transform.rotation = startRot;
                velocity.x         = 0;
                velocity.y         = 0;
                break;
            }
            if (hit.gameObject.tag == "Lever")
            {
                Lever lever = hit.gameObject.GetComponent <Lever>();
                lever.collisionWithCharacterOccurred();
                continue;
            }
            if (hit == doorCollider)
            {
                if (Random.Range(0, 12) < 2)
                {
                    Advertisement.Show();
                }
                PlayerPrefs.SetInt("nextLevel", (++level));
                string name = "Level" + level;
                Debug.Log("Lade nächstes Level: " + name);
                if (scenesInBuild.Contains(name))
                {
                    SceneManager.LoadScene(name, LoadSceneMode.Single);
                }
                //FIXME Später eine "Spielbeendet" Szene erstellen und hier verwenden
                else
                {
                    SceneManager.LoadScene("Menu", LoadSceneMode.Single);
                }
                continue;
            }
            if (hit.gameObject.tag == "Box")
            {
                BoxMover box = hit.gameObject.GetComponent <BoxMover>();
                noBox = false;
                if (Mathf.Abs(box.gameObject.transform.position.y - gameObject.transform.position.y) < 0.9f)
                {
                    box.overrideVelocityWithPlayerVelocity(velocity.x);
                }
            }
            ColliderDistance2D colliderDistance = hit.Distance(boxCollider);
            // Ensure that we are still overlapping this collider.
            // The overlap may no longer exist due to another intersected collider
            // pushing us out of this one.
            if (colliderDistance.isOverlapped)
            {
                transform.Translate(colliderDistance.pointA - colliderDistance.pointB);
                // If we intersect an object beneath us, set grounded to true.
                if (Vector2.Angle(colliderDistance.normal, Vector2.up) < 90 && velocity.y < 0)
                {
                    grounded = true;
                }
            }
        }
        if (noBox && lastBox != null)
        {
            lastBox.overrideVelocityWithPlayerVelocity(0f);
            lastBox = null;
        }
        if (noLever && lastLever != null)
        {
            lastLever.unpressLever();
            lastLever = null;
        }
        transform.Translate(velocity * Time.deltaTime);
        Camera.main.transform.position = transform.position;
    }