void directionFacing(GamepadInputHandler g)
    {
        float direction = g.GetAxis(InputAxis.MotionX);

        //Debug.Log ("Direction: " + direction);
        if (direction > 0)
        {
            isFacingRight = true;
            //sprite.flipX = true;
            transform.localScale = new Vector3(1, 1, 1);
            //wallCheckPoint.localScale = new Vector2 (wallCheckPoint.localScale.x * 1, wallCheckPoint.localScale.y);
        }
        else if (direction < 0)
        {
            isFacingRight = false;
            //sprite.flipX = false;
            transform.localScale = new Vector3(-1, 1, 1);
            //wallCheckPoint.localScale = new Vector2 (wallCheckPoint.localScale.x * -1, wallCheckPoint.localScale.y);
        }
        else
        {
            //just keep the last known variables
        }
    }
 void FixedUpdate()
 {
     xAxis = gamepad.GetAxis(InputAxis.MotionX);
     //check if we hitting a wall, this should check left and right the way we are facing
     rb.velocity = new Vector2(xAxis * speed, rb.velocity.y);
 }
Exemple #3
0
    // Update is called once per frame
    void Update()
    {
        if (isGrounded && rBody.velocity.y < 0)
        {
            isGrounded = false;
            //if(!anim.GetBool("Falling")){
            //	anim.SetBool("Falling", true);
            //}
        }

        if (explosionIndicator.explodeOnCooldown)
        {
            indicatorSprite.enabled = false;
        }
        else
        {
            indicatorSprite.enabled = true;
        }

        //Update cooldown
        pushCooldown();

        sideCheck = isFacingRight;
        //Get x-axis information for player movement
        xAxis = gamepad.GetAxis(InputAxis.MotionX);

        anim.SetFloat("Speed", Mathf.Abs(rBody.velocity.x));

        if (rBody.velocity.y < -2f)
        {
            if (!anim.GetBool("Falling"))
            {
                anim.SetBool("Falling", true);
            }
        }

        //if (xAxis > 0)
        //{
        //    if (rBody.velocity.x < 0 && isGrounded && ice != true)
        //    {
        //        rBody.velocity = new Vector2((((float)rBody.velocity.x) / 1.3f), rBody.velocity.y);
        //    }
        //}

        //if (xAxis < 0)
        //{
        //    if (rBody.velocity.x > 0 && isGrounded && ice != true)
        //    {
        //        rBody.velocity = new Vector2((((float)rBody.velocity.x) / 1.3f), rBody.velocity.y);
        //    }
        //}

        //if the rigidbody velocity is in certain direction, change to that direction
        if (rBody.velocity.x < -0.1f) //|| xAxis < 0)
        {
            //make sure sprite is flipped back
            //sprite.flipX = false;
            isFacingRight        = false;
            transform.localScale = new Vector3(-0.9f, 0.9f, 1);
            if (!particles.isEmitting && isGrounded)
            {
                particles.Play();
            }
        }
        else if (rBody.velocity.x > 0.1f) //|| xAxis > 0)
        {
            //flip sprite
            //sprite.flipX = true;
            isFacingRight        = true;
            transform.localScale = new Vector3(0.9f, 0.9f, 1);
            if (!particles.isEmitting && isGrounded)
            {
                particles.Play();
            }
        }
        else
        {
            particles.Stop();
        }

        if (isFacingRight != sideCheck)
        {
            //print("swapped sides");
            GetComponentInChildren <WallChecker>().blocksInCollider = 0;
            wallHit = false;
        }

        //if the player is facing right, check for walls on appropriate sides
        if (isFacingRight)
        {
            //raycast to detect walls for wall jumping
            //wallHit = Physics2D.Raycast(transform.position, Vector2.right, 0.3f, wallLayerMask);
            wallJumpForce.x = (-1) * Mathf.Abs(wallJumpForce.x);
        }
        else
        {
            //raycast to detect walls for wall jumping
            //wallHit = Physics2D.Raycast(transform.position, Vector2.left, 0.3f, wallLayerMask);
            wallJumpForce.x = Mathf.Abs(wallJumpForce.x);
        }

        if ((!wallHit && gamepad.GetButton(InputButton.Sprint) == ButtonState.JustPressed) || Input.GetKeyDown(KeyCode.E) && pushOnCooldown == false)
        {
            pushOnCooldown   = true;
            pushCooldownTime = coolDown;
            var hitPlayer = Physics2D.RaycastAll(transform.position, Vector2.right, 1);
            if (isFacingRight)
            {
                hitPlayer = Physics2D.RaycastAll(transform.position, Vector2.right, 1);
                Debug.DrawRay(transform.position, Vector2.right, Color.green, 20);
            }
            else
            {
                hitPlayer = Physics2D.RaycastAll(transform.position, Vector2.left, 1);
                Debug.DrawRay(transform.position, Vector2.left, Color.green, 20);
            }

            for (int i = 0; i < hitPlayer.Length; i++)
            {
                RaycastHit2D hit = hitPlayer[i];
                if (hit.collider.name == "hero 1(Clone)" && hit.collider.gameObject != this.gameObject)
                {
                    if (isFacingRight)
                    {
                        GameObject.Find("Game Manager").GetComponent <GameEngine>().SendPushPacket(new Vector2(10, 5));
                    }
                    else
                    {
                        GameObject.Find("Game Manager").GetComponent <GameEngine>().SendPushPacket(new Vector2(-10, 5));
                    }
                    print("trying to send push packet");
                }
            }
        }

        //set player velocity based on x axis
        //If player is on a certain block, apply appropriate force.
        //if (tar == true)
        //{
        //    rBody.AddForce(new Vector2(xAxis * maxRunVelocity * Time.deltaTime * tarSlowPercent, rBody.velocity.y));
        //}
        //else if (ice == true)
        //{
        //    rBody.AddForce(new Vector2(xAxis * maxRunVelocity * Time.deltaTime * iceSpeedPercent, rBody.velocity.y));
        //}
        //else
        //{
        //    rBody.AddForce(new Vector2(xAxis * maxRunVelocity * Time.deltaTime, rBody.velocity.y));
        //}



        if (rBody.velocity.x > 6)
        {
            rBody.velocity = new Vector2(6, rBody.velocity.y);
        }
        if (rBody.velocity.x < -6)
        {
            rBody.velocity = new Vector2(-6, rBody.velocity.y);
        }

        if (rBody.velocity.y > 11)
        {
            rBody.velocity = new Vector2(rBody.velocity.x, 11);
        }
        if (rBody.velocity.y < -11)
        {
            rBody.velocity = new Vector2(rBody.velocity.x, -11);
        }

        //if (xAxis == 0 && ice != true)
        //{
        //    rBody.velocity = new Vector2((((float)rBody.velocity.x) / 1.25f), rBody.velocity.y);
        //    //print(rBody.velocity.x);
        //}

        //if the player is standing on the ground
        if (isGrounded)
        {
            //if not on ground, play jump animation
            //anim.SetBool("Jumping", false);


            rBody.AddForce(new Vector2(xAxis * maxRunVelocity * Time.deltaTime, rBody.velocity.y));
            //set linear drag for normal play and reset wall jump

            hasWallJumped = false;

            //set the particles angle to up
            particles.transform.rotation = new Quaternion(0, -90, 0, particles.transform.rotation.w);

            //if the player pressed jump
            if (gamepad.GetButton(InputButton.Jump) == ButtonState.JustPressed)
            {
                //if not on ground, play jump animation
                //anim.SetBool("Jumping", true);
                anim.SetTrigger("Jumping");

                //add force to rigidbody to 'Jump'
                rBody.AddForce(jumpForce, ForceMode2D.Impulse);
                EventManager.Instance.PostNotification(EventType.PlayerJump, gameObject.transform, null);
            }
        }
        else
        {
            //EventManager.Instance.PostNotification(EventType.PlayerJump, this, null);

            //if the player is up against a wall
            if (wallHit)
            {
                //reset has wall jumped
                hasWallJumped = false;

                //set the particles angle to up
                particles.transform.rotation = new Quaternion(-80, -90, 0, particles.transform.rotation.w);

                if (!particles.isEmitting && wallHit)
                {
                    particles.Play();
                }

                //set linear drag higher for wall slide effect
                //rBody.drag = 15f;

                //if the player presses the jump button, wall jump
                if (gamepad.GetButton(InputButton.Jump) == ButtonState.JustPressed)
                {
                    rBody.velocity = new Vector2(0, 0);
                    //reset drag and hasWallJumped
                    rBody.drag    = 0;
                    hasWallJumped = true;

                    particles.Stop();

                    //if not on ground, play jump animation
                    anim.SetTrigger("Jumping");

                    //add force to rigidbody to 'Wall Jump'
                    rBody.AddForce(wallJumpForce, ForceMode2D.Impulse);
                }
                else
                {
                    //if (isFacingRight && xAxis > 0) {
                    //    rBody.velocity = new Vector2(0,rBody.velocity.y);
                    //}
                    //else if(!isFacingRight && xAxis < 0){
                    //    Debug.Log("What the fukc is happening");
                    //    rBody.velocity = new Vector2(0, rBody.velocity.y);
                    //}
                    //else {
                    //    //allows the player to move off the wall
                    //    rBody.AddForce(xAxis * (new Vector2(30, 0)), ForceMode2D.Force);
                    //}
                    rBody.AddForce(xAxis * (new Vector2(30, 0)), ForceMode2D.Force);
                }
            }
            //else if the player has activated wall jump
            else if (hasWallJumped)
            {
                //reset linear drag
                rBody.drag = 0f;

                //stop particles from playing
                particles.Stop();

                //if the desired movement is less than the max air velocity
                if (xAxis * rBody.velocity.x < maxAirVelocity)
                {
                    //add forces for smoother movement
                    rBody.AddForce(Vector2.right * xAxis * 140);
                }
                //if the absolute value of the velocity is greater than the max air velocity
                if (Mathf.Abs(rBody.velocity.x) > maxAirVelocity)
                {
                    //lock out at the max air velocity
                    rBody.velocity = new Vector2(Mathf.Sign(rBody.velocity.x) * maxAirVelocity, rBody.velocity.y);
                }
            }
            else
            {
                //stop particles from playing
                particles.Stop();

                rBody.drag = 0f;
                //rBody.velocity = new Vector2(xAxis * maxRunVelocity * Time.deltaTime, rBody.velocity.y);
                //if the desired movement is less than the max air velocity
                if (xAxis * rBody.velocity.x < maxAirVelocity)
                {
                    //add forces for smoother movement
                    rBody.AddForce(Vector2.right * xAxis * 140);
                }
                //if the absolute value of the velocity is greater than the max air velocity
                if (Mathf.Abs(rBody.velocity.x) > maxAirVelocity)
                {
                    //lock out at the max air velocity

                    rBody.velocity = new Vector2(Mathf.Sign(rBody.velocity.x) * maxAirVelocity, rBody.velocity.y);
                }
            }
        }
    }