Example #1
0
    // Update is called once per frame
    void Update()
    {
        // declaring all booleans first
        bool    jumpInput = Input.GetButtonDown("Jump" + playerNumber);
        bool    groundedR = Physics2D.OverlapCircle(groundCheckR.transform.position, 0.1f, groundMask);
        bool    groundedL = Physics2D.OverlapCircle(groundCheckL.transform.position, 0.1f, groundMask);
        bool    wallRT    = Physics2D.OverlapCircle(wallCheckRT.transform.position, 0.1f, groundMask);
        bool    wallLT    = Physics2D.OverlapCircle(wallCheckLT.transform.position, 0.1f, groundMask);
        bool    wallRB    = Physics2D.OverlapCircle(wallCheckRB.transform.position, 0.1f, groundMask);
        bool    wallLB    = Physics2D.OverlapCircle(wallCheckLB.transform.position, 0.1f, groundMask);
        Vector2 direction = new Vector2(Input.GetAxis("hAim" + playerNumber), Input.GetAxis("vAim" + playerNumber));

        playerAim.Facing aimDir = pAim.FindFacing(direction);

        levelLoop();

        if (dead)
        {
            //getShotAndDie();
            if (theRigidBody.velocity.y != 0)
            {
                sr.sprite = hurt;
            }
            else
            {
                theRigidBody.velocity = new Vector2(0f, 0f);
                sr.sprite             = killed;
            }
        }
        else
        {
            float inputX = Input.GetAxis("Horizontal" + playerNumber);
            float inputY = Input.GetAxis("Vertical" + playerNumber);

            if (groundedL || groundedR) // movement when grounded
            {
                if (inputX == 0)        // stop em if they aren't inputting anything
                {
                    theRigidBody.velocity = new Vector2(0f, theRigidBody.velocity.y);
                }
                else if (theRigidBody.velocity.x > 10 || theRigidBody.velocity.x < -10) // limit speed when going too fast aka always
                {
                    theRigidBody.velocity = new Vector2(10f * inputX, theRigidBody.velocity.y);
                }
                else // add force to move in normal circumstances
                {
                    theRigidBody.AddForce(new Vector2(inputX * walkSpeed, 0f));
                }
                if (aimDir == playerAim.Facing.None) // how to handle the sprite if they are standing still and not aiming
                {
                    if (inputY == 0)
                    {
                        sr.sprite = uidle; // no y input, dont look anywhere
                    }
                    else if (inputY > 0)
                    {
                        sr.sprite = ulookup; // look up if positive y input on left stick
                    }
                    else
                    {
                        sr.sprite = ulookdown; // look down if negative y input on left stick
                    }
                }
                lsr.sprite = lidle; // if you're on the ground and not moving then have regular legs
            }
            else // movement in air and on walls
            {
                if (theRigidBody.velocity.x > airSpeed) // limit speed by adding a negative force instead
                {
                    theRigidBody.AddForce(new Vector2(-25f, 0f));
                }
                else if (theRigidBody.velocity.x < -airSpeed) // cant have a nice 2 in one check because you can be moving in one direction but inputting the other
                {
                    theRigidBody.AddForce(new Vector2(25f, 0f));
                }
                else // add force to actually move the player. Due to no hard limit on speed in the air, walk speed is divided by 1.5
                {
                    theRigidBody.AddForce(new Vector2(inputX * (walkSpeed / 1.5f), 0f));
                }
                if (theRigidBody.velocity.y >= 0 && !(groundedL || groundedR)) // moving up?
                {
                    lsr.sprite = ljump;                                        // give em the right legs
                    if (aimDir == playerAim.Facing.None)
                    {
                        sr.sprite = ujump; // upwards looking when rising
                    }
                }
                else if (theRigidBody.velocity.y < 0 && !(groundedL || groundedR))
                {
                    if (aimDir == playerAim.Facing.None)
                    {
                        sr.sprite = ufall; // downwards looking sprite when falling
                    }
                }
            }

            if (wallRT || wallLT || wallRB || wallLB) // doing a couple of different things here...
            {
                doubleJump  = false;                  // prevent 2 in 1 walljumps
                wallTouched = 10;                     // allow walljumping briefly after leaving wall
                sr.sprite   = uwallslide;             // change sprite to wall sliding one
                lsr.sprite  = lwallslide;             // same for the legs
                if (wallLT || wallLB)
                {
                    sr.flipX  = true;  // flip while wall sliding on left walls
                    lastLeft  = true;
                    lastRight = false; // notify that the last wall touched was on the left
                }
                else
                {
                    sr.flipX  = false; // not needed most of the time but helps if theres no x input
                    lastLeft  = false;
                    lastRight = true;  // notify that the last wall touched was on the right
                }
            }
            else // if you arent touching a wall...
            {
                if (wallTouched > 0) // using wallTouched like this, the player can walljump briefly after leaving a wall. Makes movement feel better.
                {
                    wallTouched--; // decrease wallTouched while its above 0
                }
            }

            if (groundedR || groundedL) // allows the player to double jump if they run off a ledge instead of jumping off
            {
                doubleJump = true;
            }

            if (jumpInput)                  // time for some jumps
            {
                if (groundedL || groundedR) // jump from ground
                {
                    theRigidBody.AddForce(new Vector2((inputX), jumpHeight));
                    doubleJump = true;
                    notify("jump");
                }
                else
                {
                    if (doubleJump) // double jump
                    {
                        doubleJump            = false;
                        theRigidBody.velocity = new Vector2(theRigidBody.velocity.x, 0f);
                        theRigidBody.AddForce(new Vector2(40f * (inputX), (jumpHeight / 1.1f)));
                        notify("doublejump");
                    }
                    if (wallTouched > 0 && lastLeft) // wall jump with wall on left side
                    {
                        theRigidBody.velocity = new Vector2(0f, 0f);
                        theRigidBody.AddForce(new Vector2(wallPush, jumpHeight));
                        doubleJump = true;
                        sr.flipX   = false;
                        notify("jump");
                    }
                    if (wallTouched > 0 && lastRight) // wall jump with wall on right side
                    {
                        theRigidBody.velocity = new Vector2(0f, 0f);
                        theRigidBody.AddForce(new Vector2(-wallPush, jumpHeight));
                        doubleJump = true;
                        sr.flipX   = true;
                        notify("jump");
                    }
                }
            }
            if (wallTouched == 0) // only need to worry about what the last wall touched was while you can walljump
            {
                lastLeft  = false;
                lastRight = false;
            }
            // a simple check to make sure the player is facing the way they're moving
            // only happens if there is no aim input
            if (aimDir == playerAim.Facing.None)
            {
                if (inputX < 0)
                {
                    sr.flipX = true; // flip if going left
                }
                if (inputX > 0)
                {
                    sr.flipX = false; // unflip if going right
                }
            }
            if (theRigidBody.velocity.y != 0 && !(wallRT || wallLT || wallRB || wallLB) && !(groundedL || groundedR))
            {
                lsr.sprite = ljump; // keeps the legs from being invisible after sliding off a wall w/o jumping
            }
        }
    }
Example #2
0
    // Update is called once per frame
    void Update()
    {
        Vector2 direction = new Vector2(Input.GetAxis("hAim" + playerNumber), Input.GetAxis("vAim" + playerNumber));

        aimDir = pAim.FindFacing(direction);

        // this is the shittiest thing I really wish there was a better way
        if (Input.GetAxisRaw("Shoot" + playerNumber) != 0 && !isAxisInUse) // if you pull the trigger and its not already pulled
        {
            isAxisInUse = true;                                            // makes it so you wont fire multiple times in a single pull
            activeTimer = 20;                                              // this gets decremented to set isAxisInUse to false, more on that later down
            if (aimDir == playerAim.Facing.None && ammo >= 0)              // aim nowhere, do nothing
            {
            }
            else
            {
                ammo--;
                if (aimDir == playerAim.Facing.Down && ammo >= 0)                                                                               // shooting DOWN
                {
                    theRigidBody.velocity = new Vector2(theRigidBody.velocity.x, 0f);                                                           // reset y velocity first
                    theRigidBody.AddForce(new Vector2(0f, shotForce));                                                                          // add the force to push player in opposite direction
                    ShotRenderer.transform.Rotate(Vector3.forward * -90);                                                                       // rotate the shot to make it face the right way
                    ShotRenderer.transform.position = new Vector2(ShotRenderer.transform.position.x, ShotRenderer.transform.position.y - 2.0f); // put the sprite in the right spot so it lines up with the gun
                }
                else if (aimDir == playerAim.Facing.Up && ammo >= 0)                                                                            // shooting UP
                {
                    theRigidBody.velocity = new Vector2(theRigidBody.velocity.x, 0f);
                    theRigidBody.AddForce(new Vector2(0f, -shotForce));
                    ShotRenderer.transform.Rotate(Vector3.forward * 90);
                    ShotRenderer.transform.position = new Vector2(ShotRenderer.transform.position.x, ShotRenderer.transform.position.y + 2.26f);
                }
                else if (aimDir == playerAim.Facing.Right && ammo >= 0) // shooting RIGHT
                {
                    theRigidBody.velocity = new Vector2(0f, theRigidBody.velocity.y);
                    theRigidBody.AddForce(new Vector2(-shotForce * 1.5f, 0f));
                    ShotRenderer.transform.position = new Vector2(ShotRenderer.transform.position.x + 2f, ShotRenderer.transform.position.y + 0.2f);
                }
                else if (aimDir == playerAim.Facing.Left && ammo >= 0) // shooting LEFT
                {
                    theRigidBody.velocity = new Vector2(0f, theRigidBody.velocity.y);
                    theRigidBody.AddForce(new Vector2(shotForce * 1.5f, 0f));
                    ShotRenderer.transform.position = new Vector2(ShotRenderer.transform.position.x - 2f, ShotRenderer.transform.position.y + 0.2f);
                    ShotRenderer.transform.Rotate(Vector3.forward * 180);
                }
                else if (aimDir == playerAim.Facing.DownLeft && ammo >= 0) // shooting DOWN LEFT
                {
                    theRigidBody.velocity = new Vector2(0f, 0f);
                    theRigidBody.AddForce(new Vector2(shotForce / 1.2f, shotForce / 1.2f));
                    ShotRenderer.transform.Rotate(Vector3.forward * -135);
                    ShotRenderer.transform.position = new Vector2(ShotRenderer.transform.position.x - 1.48f, ShotRenderer.transform.position.y - 1.34f);
                }
                else if (aimDir == playerAim.Facing.DownRight && ammo >= 0) // shooting DOWN RIGHT
                {
                    theRigidBody.velocity = new Vector2(0f, 0f);
                    theRigidBody.AddForce(new Vector2(-shotForce / 1.2f, shotForce / 1.2f));
                    ShotRenderer.transform.Rotate(Vector3.forward * -45);
                    ShotRenderer.transform.position = new Vector2(ShotRenderer.transform.position.x + 1.48f, ShotRenderer.transform.position.y - 1.34f);
                }
                else if (aimDir == playerAim.Facing.UpLeft && ammo >= 0) // shooting UP LEFT
                {
                    theRigidBody.velocity = new Vector2(0f, 0f);
                    theRigidBody.AddForce(new Vector2(shotForce / 1.2f, -shotForce / 1.2f));
                    ShotRenderer.transform.Rotate(Vector3.forward * 135);
                    ShotRenderer.transform.position = new Vector2(ShotRenderer.transform.position.x - 1.56f, ShotRenderer.transform.position.y + 1.8f);
                }
                else if (aimDir == playerAim.Facing.UpRight && ammo >= 0) // shooting UP RIGHT
                {
                    theRigidBody.velocity = new Vector2(0f, 0f);
                    theRigidBody.AddForce(new Vector2(-shotForce / 1.2f, -shotForce / 1.2f));
                    ShotRenderer.transform.Rotate(Vector3.forward * 45);
                    ShotRenderer.transform.position = new Vector2(ShotRenderer.transform.position.x + 1.56f, ShotRenderer.transform.position.y + 1.8f);
                }
                if (ammo < 0 && aimDir != playerAim.Facing.None) // do nothing if you're out of ammo
                {
                    animationController.SetFloat("shoot", 0);
                }
                else
                {
                    animationController.SetFloat("shoot", Input.GetAxisRaw("Shoot" + playerNumber)); // play the shooting animation
                    hitbox.enabled = true;                                                           // enable the shot hitbox
                    notify("shoot");
                }
            }
        }
        if (Input.GetButtonDown("Reload" + playerNumber)) // reload, pretty self explanatory
        {
            ammo = 2;
        }
        if (isAxisInUse)         // decrement the activeTimer if axis is in use
        {
            if (activeTimer > 0) // I do this as a way to make sure the sprite and hitbox stay in proper positions
            {
                activeTimer--;   //  its a pretty bad way to do it
            }
        }
        if (activeTimer == 0) // set axis to free if the "timer" is up
        {
            isAxisInUse = false;
        }
        if (Input.GetAxisRaw("Shoot" + playerNumber) == 0)                                                   // if you're not shooting, make sure the animation doesnt play
        {
            animationController.SetFloat("shoot", 0);                                                        // makes it so the shot effect goes away when its over
        }
        if (!isAxisInUse)                                                                                    // if the axis isnt being used, then you're not shooting so...
        {
            hitbox.enabled = false;                                                                          // disable the hitbox
            ShotRenderer.transform.position = new Vector2(theRigidBody.position.x, theRigidBody.position.y); // keep the shot on the player so its ready to render
            ShotRenderer.transform.rotation = Quaternion.identity;                                           // set the rotation of the shot back to 0
        }
        if (pMove.dead)
        {
            isAxisInUse = true;
            ammo        = 2;
        }
        if (score >= 1)
        {
            victoryCanvas.enabled = true;
        }
        else
        {
            victoryCanvas.enabled = false;
        }
    }