// Update is called once per frame void Update() { //check which direction we are facing directionFacing(gamepad); float pushOffFacingDirection = isFacingRight ? 1 : -1; if (gamepad.GetButton(InputButton.Jump) == ButtonState.JustPressed) { if (isGrounded) { rb.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse); //rb.velocity = new Vector2(80,jumpForce); wallSliding = false; rb.drag = 0; Debug.Log("is jumped"); } } //wall sliding check and setting sliding values if (!isGrounded && wallCheck && rb.velocity.y < 0) { //Debug.Log (rb.velocity.y); wallSliding = true; if (rb.velocity.y < (wallSlideSpeedMax * -1)) { //rb.velocity = new Vector2(rb.velocity.x,(wallSlideSpeedMax*-1)); rb.drag = 15f; } } }
// 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); } } } }