Example #1
0
    void Update()
    {
        // Controls
        int h = (rebind.GetKey("Right") ? 1 : 0) + (rebind.GetKey("Left") ? -1 : 0);
        int v = (rebind.GetKey("Up") ? 1 : 0) + (rebind.GetKey("Down") ? -1 : 0);

        if (h != 0 || v != 0)
        {
            anim.SetBool("Walking", true);
        }
        else
        {
            anim.SetBool("Walking", false);
        }

        rigidbody2D.velocity = new Vector2(h, v).normalized *unit.speed;

        if (h != 0 || v != 0)
        {
            transform.localEulerAngles = Utils.RotateTo(h, v);
        }

        if (rebind.GetKeyDown("Attack"))
        {
            Attack();
        }
    }
Example #2
0
    // Input Handler
    private void InputMovement()
    {
        // Basic movement
        if (canMove)
        {
            // Rebinding
            if (keys.GetKey("P" + player.getPlayerID() + "Right") && CanMoveForward(lookingDir))
            {
                lookingRight         = true;
                rigidbody2D.velocity = new Vector2(speedBase * speedMod, rigidbody2D.velocity.y);
            }
            else if (keys.GetKey("P" + player.getPlayerID() + "Left") && CanMoveForward(lookingDir))
            {
                lookingRight         = false;
                rigidbody2D.velocity = new Vector2(-speedBase * speedMod, rigidbody2D.velocity.y);
            }
            else
            {
                rigidbody2D.velocity = new Vector2(0f, rigidbody2D.velocity.y);
            }
        }

        // Update direction
        if (lookingRight && transform.localScale.x != 1f)
        {
            transform.localScale = new Vector3(1f, transform.localScale.y, 1f);
            lookingDir           = Vector2.right;
        }
        else if (!lookingRight && transform.localScale.x != -1f)
        {
            transform.localScale = new Vector3(-1f, transform.localScale.y, 1f);
            lookingDir           = -Vector2.right;
        }

        // Rope
        bool kup   = keys.GetKey("P" + player.getPlayerID() + "Up");
        bool kdown = keys.GetKey("P" + player.getPlayerID() + "Down");

        if (kup && !onRope && overRope)
        {
            onRope = true;
            OnRope();
        }

        if (onRope)
        {
            if (kup)
            {
                rigidbody2D.velocity = new Vector2(0f, ropeVelocityY);
            }

            if (kdown)
            {
                rigidbody2D.velocity = new Vector2(0f, -ropeVelocityY);
            }

            if ((kup && kdown) || (!kup && !kdown))
            {
                rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, 0f);
            }
        }

        // Jetpack
        // Jetpack counts time when is activated
        // When it turns deactivated or the max time using it reaches
        // it begins to count down 'til it reaches zero
        // It can only be reactivated when the time reaches zero
        if (jetActive)
        {
            if (jetCurTime < jetMaxTime)
            {
                jetCurTime += Time.deltaTime;
            }
            else
            {
                jetCurTime = jetMaxTime;
                jetActive  = false;
            }
        }
        else
        {
            if (jetCurTime > 0f)
            {
                jetCurTime -= Time.deltaTime;
            }
            else
            {
                jetCurTime   = 0f;
                jetCanActive = true;
            }
        }

        if (!jetFreeze)
        {
            if (keys.GetKeyDown("P" + player.getPlayerID() + "Jet") && !jetActive && jetCanActive)
            {
                jetActive    = true;
                jetCanActive = false;
            }

            if (keys.GetKeyUp("P" + player.getPlayerID() + "Jet"))
            {
                jetActive = false;
            }

            if (keys.GetKey("P" + player.getPlayerID() + "Jet") && jetActive)
            {
                rigidbody2D.AddForce(Vector2.up * jetForceBase * jetForceMod);
                IsGrounded();
            }
        }
    }