void Update() { Vector2 input = new Vector2(CrossPlatformInputManager.GetAxisRaw("Horizontal"), CrossPlatformInputManager.GetAxisRaw("Vertical")); int wallDirX = (controller.collisions.left) ? -1 : 1; float targetVelocityX = input.x * moveSpeed; velocity.x = Mathf.SmoothDamp(velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below)?accelerationTimeGrounded:accelerationTimeAirborne); bool wallSliding = false; if ((controller.collisions.left || controller.collisions.right) && !controller.collisions.below && velocity.y < 0) { wallSliding = true; if (velocity.y < -wallSlideSpeedMax) { velocity.y = -wallSlideSpeedMax; } if (timeToWallUnstick > 0) { velocityXSmoothing = 0; velocity.x = 0; if (input.x != wallDirX && input.x != 0) { timeToWallUnstick -= Time.deltaTime; } else { timeToWallUnstick = wallStickTime; } } else { timeToWallUnstick = wallStickTime; } } if (controller.collisions.above || controller.collisions.below) { velocity.y = 0; } if (CrossPlatformInputManager.GetButtonDown("Jump")) { if (wallSliding) { if (wallDirX == input.x) { velocity.x = -wallDirX * wallJumpClimb.x; velocity.y = wallJumpClimb.y; } else if (input.x == 0) { velocity.x = -wallDirX * wallJumpOff.x; velocity.y = wallJumpOff.y; } else { velocity.x = -wallDirX * wallLeap.x; velocity.y = wallLeap.y; } } if (controller.collisions.below) { velocity.y = jumpVelocity; } } velocity.y += gravity * Time.deltaTime; controller.Move(velocity * Time.deltaTime); }
void Update() { //Begin Kyle Code //stops physics if player is not local instance, can be overriden if (!photonView.IsMine && !forceMovement) { return; } //End Kyle Code checkFalling(); Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); int wallDirX = (controller.collisions.left) ? -1 : 1; if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.D)) { if (input.x > 0 && canMove) { characterScale.x = characterScaleX; Vector3 t = nameText.transform.localScale; nameText.transform.localScale = new Vector3(1.603427f, t.y, t.z); ani.SetTrigger("run"); } if (input.x < 0 && canMove) { Vector3 t = nameText.transform.localScale; nameText.transform.localScale = new Vector3(-1.603427f, t.y, t.z); characterScale.x = -characterScaleX; ani.SetTrigger("run"); } } transform.localScale = characterScale; float targetVelocityX = input.x * moveSpeed; if (canMove) { velocity.x = Mathf.SmoothDamp(velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below) ? accelerationTimeGrounded : accelerationTimeAirborne); } bool wallSliding = false; if ((controller.collisions.left || controller.collisions.right) && (!controller.collisions.below) && (Input.GetKey(KeyCode.Q))) { velocity.y = 0; } if ((controller.collisions.left || controller.collisions.right) && (!controller.collisions.below && velocity.y < 0) && (wallDirX == input.x)) { wallSliding = true; if (velocity.y < -wallSlideSpeedMax) { velocity.y = -wallSlideSpeedMax; } if (timeToWallUnstick > 0) { velocityXSmoothing = 0; velocity.x = 0; if (input.x != wallDirX && input.x != 0) { timeToWallUnstick -= Time.deltaTime; } else { timeToWallUnstick = wallStickTime; } } else { timeToWallUnstick = wallStickTime; } } if (Input.GetKeyDown(KeyCode.Space)) { ani.SetTrigger("jump"); if (controller.collisions.left || controller.collisions.right) { if (wallDirX == input.x) { velocity.x = -wallDirX * wallJumpClimb.x; velocity.y = wallJumpClimb.y; } else if (input.x == 0) { velocity.x = -wallDirX * wallLeap.x; velocity.y = wallLeap.y; StartCoroutine(movementLockout2(.3f, false)); } else { velocity.x = -wallDirX * wallLeap.x; velocity.y = wallLeap.y; StartCoroutine(movementLockout2(.3f, false)); } } if (controller.collisions.below) { velocity.y = maxJumpVelocity; } } if (Input.GetKeyUp(KeyCode.Space)) { if (velocity.y > minJumpVelocity) { velocity.y = minJumpVelocity; } } if (onRope && Input.GetKeyUp(KeyCode.W)) { velocity.y = 0; ani.SetTrigger("climb_idle"); } if (onRope && Input.GetKeyUp(KeyCode.S)) { velocity.y = 0; ani.SetTrigger("climb_idle"); } if (onRope && Input.GetKeyDown(KeyCode.W)) { velocity.y = ropeVelocity; ani.SetTrigger("climb"); } if (onRope && Input.GetKeyDown(KeyCode.S)) { velocity.y = -ropeVelocity; ani.SetTrigger("climb"); } if (!onRope) { velocity.y += gravity * Time.deltaTime; } controller.Move(velocity * Time.deltaTime, input); if (controller.collisions.above || controller.collisions.below) { velocity.y = 0; } if (!Input.anyKey && !onRope && canMove) { ani.SetTrigger("idle"); } }
void Update() { Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); int wallDirX = (controller.collisions.left) ? -1:1; if (Mathf.Sign(velocity.x) == -1) { Debug.Log("Moving Left"); //transform.localRotation = Quaternion.Euler(0, 180, 0); } if (Mathf.Sign(velocity.x) == 1) { Debug.Log("Moving Right"); //transform.localRotation = Quaternion.Euler(0, 0, 0); } float targetVelocityX = input.x * moveSpeed; velocity.x = Mathf.SmoothDamp(velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below)? accelerationTimeGrounded:accelerationTimeAirborne); bool wallSliding = false; if ((controller.collisions.left || controller.collisions.right) && !controller.collisions.below && velocity.y < 0) { wallSliding = true; if (velocity.y < -wallSlideSpeedMax) { velocity.y = -wallSlideSpeedMax; } if (timeToWallUnstick > 0) { velocityXSmoothing = 0; velocity.x = 0; if (input.x != wallDirX && input.x != 0) { timeToWallUnstick -= Time.deltaTime; } else { timeToWallUnstick = wallStickTime; } } else { timeToWallUnstick = wallStickTime; } } if (controller.collisions.above || controller.collisions.below) { velocity.y = 0; } if (Input.GetKeyDown(KeyCode.Space)) //if space bar pressed and on ground, jump! { if (wallSliding) { if (wallDirX == input.x) { velocity.x = -wallDirX * wallJumpClimb.x; velocity.y = wallJumpClimb.y; } else if (input.x == 0) { velocity.x = -wallDirX * wallJumpOff.x; velocity.y = wallJumpOff.y; } else { velocity.x = -wallDirX * wallLeap.x; velocity.y = wallLeap.y; } } if (controller.collisions.below) { velocity.y = jumpVelocity; Debug.Log("Im Jumping!"); Debug.Log(velocity.y.ToString()); } } velocity.y += gravity * Time.deltaTime; controller.Move(velocity * Time.deltaTime, input); }
// Update is called once per frame void Update() { controller2D.Move(direction * velocity); }
// Update is called once per frame void Update() { // get player input for direction and which direction they are colliding inot the wall Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); int wallDirX = (controller.collisions.left) ? -1 : 1; // currently not sliding on wall bool wallSliding = false; // check if sliding on wall if ((controller.collisions.left || controller.collisions.right) && !controller.collisions.below && velocity.y < 0) { // currently colliding into wall wallSliding = true; // slide down wall slowly if (velocity.y < -wallSlideSpeedMax) { velocity.y = -wallSlideSpeedMax; } } // stop when hitting object from above or below if (controller.collisions.above || controller.collisions.below) { velocity.y = 0; } // player wants to move left if (Input.GetKey(moveLeft)) { // check if player can sprint if (playerOneStamina.currentStamina > 0 && Input.GetKey(sprint) && playerOneStamina.fiftyStamina) { // move player faster to sprint and decrease stamina input.x = -sprintSpeed; playerOneStamina.decreaseStamina(); } // do not sprint else { // move player at normal speed input.x = -moveSpeed; // increase player stamina when not sprinting if (playerOneStamina.currentStamina < playerOneStamina.maxStamina) { playerOneStamina.increaseStamina(); } } // face bullet to the left bulletRotation.eulerAngles = new Vector3(0, 0, 90); resetBulletDirection(); bulletLeft = true; // face sowrd to the left swordDirection = -1f; swordRotation.eulerAngles = new Vector3(0, 0, 90); // face player to the left redPlayer.flipX = false; } // player want to move right else if (Input.GetKey(moveRight)) { // check if player can sprint if (playerOneStamina.currentStamina > 0 && Input.GetKey(sprint) && playerOneStamina.fiftyStamina) { // move player faster to sprint and decrease stamina input.x = sprintSpeed; playerOneStamina.decreaseStamina(); } // do not sprint else { // move player at normal speed input.x = moveSpeed; // increase player stamina when not sprinting if (playerOneStamina.currentStamina < playerOneStamina.maxStamina) { playerOneStamina.increaseStamina(); } } // face bullet to the right bulletRotation.eulerAngles = new Vector3(0, 0, -90); resetBulletDirection(); bulletRight = true; // face sword to the right swordDirection = 1f; swordRotation.eulerAngles = new Vector3(0, 0, 0); // face player to the right redPlayer.flipX = true; } // player does not want to move else { // do not move player input.x = 0; // increase player stamina if (playerOneStamina.currentStamina < playerOneStamina.maxStamina) { playerOneStamina.increaseStamina(); } } // player wants to shoot diaginally up and right if (Input.GetKey(shootTopRight) && Time.time > nextFire) { // face bullet up and right bulletRotation.eulerAngles = new Vector3(0, 0, -45); resetBulletDirection(); bulletTopRight = true; // create three different bullets Instantiate(bullet, transform.position, bulletRotation); Instantiate(bullet, new Vector3(transform.position.x, transform.position.y + 0.2f, transform.position.z), bulletRotation); Instantiate(bullet, new Vector3(transform.position.x, transform.position.y - 0.2f, transform.position.z), bulletRotation); // add cooldown to fire rate nextFire = Time.time + fireRate; } // player wants to shoot diaginally down and right else if (Input.GetKey(shootBottomRight) && Time.time > nextFire) { // face bullet down and right bulletRotation.eulerAngles = new Vector3(0, 0, -135); resetBulletDirection(); bulletBottomRight = true; // create three different bullets Instantiate(bullet, transform.position, bulletRotation); Instantiate(bullet, new Vector3(transform.position.x, transform.position.y + 0.2f, transform.position.z), bulletRotation); Instantiate(bullet, new Vector3(transform.position.x, transform.position.y - 0.2f, transform.position.z), bulletRotation); // add cooldown to fire rate nextFire = Time.time + fireRate; } // player wants to shoot diaginally up and left else if (Input.GetKey(shootTopLeft) && Time.time > nextFire) { // face bullet up and left bulletRotation.eulerAngles = new Vector3(0, 0, 45); resetBulletDirection(); bulletTopLeft = true; // create three different bullets Instantiate(bullet, transform.position, bulletRotation); Instantiate(bullet, new Vector3(transform.position.x, transform.position.y + 0.2f, transform.position.z), bulletRotation); Instantiate(bullet, new Vector3(transform.position.x, transform.position.y - 0.2f, transform.position.z), bulletRotation); // add cooldown to fire rate nextFire = Time.time + fireRate; } // player wants to shoot diaginally down and left else if (Input.GetKey(shootBottomLeft) && Time.time > nextFire) { // face bullet down and left bulletRotation.eulerAngles = new Vector3(0, 0, 135); resetBulletDirection(); bulletBottomLeft = true; // create three different bullets Instantiate(bullet, transform.position, bulletRotation); Instantiate(bullet, new Vector3(transform.position.x, transform.position.y + 0.2f, transform.position.z), bulletRotation); Instantiate(bullet, new Vector3(transform.position.x, transform.position.y - 0.2f, transform.position.z), bulletRotation); // add cooldown to fire rate nextFire = Time.time + fireRate; } // player wants to jump if (Input.GetKey(jump)) { // check if sliding on wall if (wallSliding) { // player jumps to same wall they are sliding on if (wallDirX == input.x) { velocity.x = -wallDirX * wallJumpClimb.x; velocity.y = wallJumpClimb.y; } // player jumps off wall else if (input.x == 0) { velocity.x = -wallDirX * wallJumpOff.x; velocity.y = wallJumpOff.y; } // player jumps off wall and goes in opposite direction of wall else { velocity.x = -wallDirX * wallLeap.x; velocity.y = wallLeap.y; } } // be able to jump when on ground if (controller.collisions.below) { velocity.y = jumpVelocity; } } // player wants to shoot forward if (Input.GetKey(shoot) && Time.time > nextFire) { // create three different bullets Instantiate(bullet, transform.position, bulletRotation); Instantiate(bullet, new Vector3(transform.position.x, transform.position.y + 0.2f, transform.position.z), bulletRotation); Instantiate(bullet, new Vector3(transform.position.x, transform.position.y - 0.2f, transform.position.z), bulletRotation); // add cooldown to fire rate nextFire = Time.time + fireRate; } // player wants to use sword if (Input.GetKey(useSword) && Time.time > nextFire) { // create sword Instantiate(sword, new Vector3(transform.position.x + swordDirection, transform.position.y, transform.position.z), swordRotation); // add cooldown to fire rate nextFire = Time.time + fireRate; } // move player float targetVelocityX = input.x * moveSpeed * Time.deltaTime; velocity.x = Mathf.SmoothDamp(velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below)? accelerationTimeGrounded: accelationTimeAirborne); velocity.y += gravity * Time.deltaTime; controller.Move(velocity * Time.deltaTime); }
public void handleMovement() { bool wallSliding = false; //Retrieving input from controller stick Vector2 input = new Vector2(Input.GetAxis("LeftJoystickHorizontal"), Input.GetAxis("LeftJoystickVertical")); // Cheking if deadzone is bigger than input for neutralizing movement if (input.magnitude < DEADZONE_MOVEMENT) { input = Vector2.zero; } int wallDirx = (controller.collisions.left)? -1 : 1; if ((controller.collisions.left || controller.collisions.right) && !controller.collisions.below && velocity.y < 0) { wallSliding = true; //playerState.wallJumping = false; wallTimer = 0; if (velocity.y < -wallSlideSpeedMax) { velocity.y = -wallSlideSpeedMax; } } if (controller.collisions.above || controller.collisions.below) { velocity.y = 0; } if (Input.GetButtonDown("BButton")) { if (wallSliding) { if (wallDirx == input.x) { velocity.x = -wallDirx * wallJumpClimb.x; velocity.y = wallJumpClimb.y; } else { velocity.x = -wallDirx * wallLeap.x; velocity.y = wallLeap.y; //wallTimeJump = .5f; } playerState.wallJumping = true; } if (controller.collisions.below) { velocity.y = maxJumpVelocity; } } if (Input.GetButtonUp("BButton")) { if (velocity.y > minJumpVelocity) { velocity.y = minJumpVelocity; } } // Flipping the character and animations if (input.x > 0 && !facingRight) { playerState.facingRight = true; Flip(); } else if (input.x < 0 && facingRight) { playerState.facingRight = false; Flip(); } if (playerState.wallJumping) { if (wallTimer <= wallTimeJump) { wallTimer += Time.deltaTime; float targetVelocityX = input.x * moveSpeed; velocity.x = Mathf.SmoothDamp(velocity.x, targetVelocityX, ref velocityXSmoothing, accelerationTimeAirborne); } else { playerState.wallJumping = false; wallTimeJump = .15f; wallTimer = 0; } } else { // Adding move speed to the character velocity.x = input.x * moveSpeed; anim.SetFloat("Speed", Mathf.Abs(velocity.x)); } velocity.y += gravity * Time.deltaTime; if (!playerState.dashing) { if (Input.GetButtonDown("XButton")) { Debug.Log("DASH"); playerState.dashing = true; timer = 0; } } else { if (timer <= dashDuration) { if (playerState.facingRight) { velocity.x = dashSpeed; } else { velocity.x = -dashSpeed; } timer += Time.deltaTime; velocity.y = 0; } else { playerState.dashing = false; //velocity.x = 3; } } //velocity.y += gravity * Time.deltaTime; controller.Move(velocity * Time.deltaTime); }
private void UpdateMovement() { Vector2 input = new Vector2(Input.GetAxis("Horizontal" + playerID), Input.GetAxis("Vertical" + playerID)); int wallDirX = (controller.collisions.left) ? -1 : 1; float newRotationY = rotationContainer.eulerAngles.y; if (Input.GetAxis("Horizontal" + playerID) > 0.0f) { newRotationY = -900.0f; } else { newRotationY = 0.0f; } rotationContainer.eulerAngles = Vector3.Lerp(transform.rotation.eulerAngles, new Vector3(0.0f, newRotationY, 0.0f), modelRotationSpeed * Time.deltaTime); if (Mathf.Abs(input.x) > 0.1f) //Right rotation animation { //var newRoation = Quaternion.LookRotation(moveDirection); //model.transform.rotation = Quaternion.Slerp(model.transform.rotation, newRoation, modelRotationSpeed * Time.deltaTime); animator.SetFloat("MovementSpeed", Mathf.Abs(input.x)); } else { animator.SetFloat("MovementSpeed", 0); } float targetVelocityX = input.x * movementSpeed; velocity.x = Mathf.SmoothDamp(velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below) ? accelerationTimeGround : accelerationTimeAir); bool wallSliding = false; if ((controller.collisions.left || controller.collisions.right) && !controller.collisions.below && velocity.y < 0) { wallSliding = true; if (velocity.y < -wallSlideSpeedMax) { velocity.y = -wallSlideSpeedMax; } if (timeToWallUnstick > 0) { velocityXSmoothing = 0; velocity.x = 0; if (input.x != wallDirX && input.x != 0) { timeToWallUnstick -= Time.deltaTime; } else { timeToWallUnstick = wallStickTime; } } else { timeToWallUnstick = wallStickTime; } } //Head check if (controller.collisions.above) { velocity.y = 0; } //Ground check if (controller.collisions.below) { velocity.y = 0; jumpsRemaining = numberOfJumps; } if (Input.GetAxis("Jump" + playerID) > 0.0f) { bool walljumped = false; float jumpAxisVelocity = Input.GetAxis("Jump" + playerID); if (wallSliding) { walljumped = true; if (wallDirX == input.x) { velocity.x = jumpAxisVelocity * -wallDirX * wallJumpClimb.x; velocity.y = jumpAxisVelocity * wallJumpClimb.y; } else if (input.x == 0) { velocity.x = jumpAxisVelocity * -wallDirX * wallJumpOff.x; velocity.y = jumpAxisVelocity * wallJumpOff.y; } else { velocity.x = jumpAxisVelocity * -wallDirX * wallLeap.x; velocity.y = jumpAxisVelocity * wallLeap.y; } } if (controller.collisions.below || ((Time.time - lastJumpTime > jumpDelay) && (jumpsRemaining > 0))) { lastJumpTime = Time.time; float deacrease = (numberOfJumps - jumpsRemaining) * extraJumpDecrease; velocity.y = jumpVelocity - deacrease; if (walljumped && !wallJumpsCountAsDoubleJumps) { jumpsRemaining++; } jumpsRemaining--; animator.SetTrigger("Jump"); } } velocity.y += gravity * Time.deltaTime; controller.Move(velocity * Time.deltaTime); }
void Update() { if (!isServer) { return; } Vector2 input = movementAxis; int wallDirX = (controller.collisions.left) ? -1 : 1; //flip sprite if (movementAxis.x > 0 && !facingRight) { flip(); } else if (movementAxis.x < 0 && facingRight) { flip(); } float targetVelocityX = input.x * moveSpeed; velocity.x = Mathf.SmoothDamp(velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below) ? accelerationTimeGrounded : accelerationTimeAirborne); //walking if (controller.collisions.below) { currentPlatform = controller.collisions.platform; if (targetVelocityX < 0) { if (facingRight) { animator.setWalkBackward(); } else { animator.setWalkForward(); } } else if (targetVelocityX > 0) { if (!facingRight) { animator.setWalkBackward(); } else { animator.setWalkForward(); } } else { animator.setIdle(); } } //jumping if (buttonPressedJump) { if (controller.collisions.below || jump == 1) { velocity.y = maxJumpVelocity; decelerating = false; if (jump == 1) { //syncPlayer.CmdSyncJetpack(true); } } if (jump == 0) { jump = 1; } else if (jump == 1) { jump = 2; } } else if (buttonReleasedJump) { decelerating = true; //syncPlayer.CmdSyncJetpack(false); } if (velocity.y < 0) { animator.setFall(); //syncPlayer.CmdSyncJetpack(false); } else if (velocity.y > 0) { animator.setJump(); } //decelerate after jump if (decelerating && velocity.y > minJumpVelocity) { if (velocity.y - jumpDeceleration > minJumpVelocity) { velocity.y -= jumpDeceleration; } else { velocity.y = minJumpVelocity; } } //gravity, with max fall speed if (velocity.y > maxFallSpeed) { if (velocity.y - (gravity * Time.deltaTime) > maxFallSpeed) { velocity.y -= gravity * Time.deltaTime; } else { velocity.y = maxFallSpeed; } } //if (jump != 0 && controller.collisions.below) { // Debug.Log("landing"); // animator.setIdle(); //} controller.Move(velocity * Time.deltaTime, input); if (controller.collisions.below) { velocity.y = 0; decelerating = false; jump = 0; } else { if (controller.collisions.above) { velocity.y = 0; decelerating = false; } } //shooting if (buttonPressedShoot) { //gun.shoot(); } }
void Update() { // Takes all horizontal input from the player when moving left or right. Vector2 input = new Vector2(Input.GetAxisRaw("HorizontalMove"), Input.GetAxisRaw("VerticalMove")); // If the input is moving the player right while the player is facing left... if (Input.GetAxisRaw("HorizontalMove") < 0 && transform.localScale.x > 0) { // ... flip the player. Flip(); } // Otherwise if the input is moving the player left while the player is facing right... else if (Input.GetAxisRaw("HorizontalMove") > 0 && transform.localScale.x < 0) { // ... flip the player. Flip(); } // Determines collision direction when colliding against walls. int wallDirectionX = (controller.collisions.left) ? -1 : 1; // Movement calculations when the Player is moving left or right. float targetVelocityX = input.x * moveSpeed; float standardMovementVelocity = Mathf.SmoothDamp(velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below) ? accelerationTimeOnTheGround : accelerationTimeInTheAir); velocity.x = standardMovementVelocity; // Perform a check to see if the Player hasn't been knockback yet. if (playerKnockbackStatus.wasKnockedBack == false) { // Controls various aspects when moving against and on walls. bool wallSliding = false; if ((controller.collisions.left || controller.collisions.right) && !controller.collisions.below && velocity.y < 0) { // Tells the game that Player is sliding on a wall. wallSliding = true; // Prevents the velocity from going further then wallSlideSpeedMax. if (velocity.y < -wallSlideSpeedMax) { velocity.y = -wallSlideSpeedMax; } // Determines how fast the Player unsticks from walls. if (timeToWallUnstick > 0) { velocityXSmoothing = 0; velocity.x = 0; // Gradually pushes the player off of the wall. if (input.x != wallDirectionX && input.x != 0) { timeToWallUnstick -= Time.deltaTime; } // Pushes the Player off of the wall immediately. else { timeToWallUnstick = wallStickTime; } } // Pushes the Player off of the wall immediately. else { timeToWallUnstick = wallStickTime; } } // For jumping on walls and for maximum height. if (Input.GetButtonDown("Jump")) { // Code only executes when the Player is on a wall. if (wallSliding) { // Executes only when the Player is jumping towards a wall. if (wallDirectionX == input.x) { velocity.x = -wallDirectionX * wallJumpClimb.x; velocity.y = wallJumpClimb.y; } // Executes only when the Player is inactive on a wall. else if (input.x == 0) { velocity.x = -wallDirectionX * wallJumpOff.x; velocity.y = wallJumpOff.y; } // Executes only when the Player is jumping away from a wall. else { velocity.x = -wallDirectionX * wallLeap.x; velocity.y = wallLeap.y; } // Resets air dash counter as the Player is on a wall. // lockingAllAirDashes = 0; // Prevents the Player from dashjumping by this point. if (!Input.GetButton("Dash")) { lockingAllDashJumping++; } } // Code only executes when the Player is on the ground. if (controller.collisions.below) { // Used to provide the maximum jumping distance for the Player. velocity.y = maximumJumpVelocity; // Resets air dash counter as the Player lands on the ground. // lockingAllAirDashes = 0; // Prevents the Player from dashjumping by this point. if (!Input.GetButton("Dash")) { lockingAllDashJumping++; } } } // For jumping with minimum height. if (Input.GetButtonUp("Jump")) { // Used to provide the minimum jumping distance for the Player. if (velocity.y > minimumJumpVelocity) { velocity.y = minimumJumpVelocity; } // Resets air dash counter as the Player lands on the ground. // lockingAllAirDashes = 0; // Prevents the Player from dashjumping by this point. if (!Input.GetButton("Dash")) { lockingAllDashJumping++; } } // If dashing jumping, allow the player to continue their dashing momentum. if ((Input.GetButton("Dash") && Input.GetButton("Jump")) && lockingAllDashJumping < 1) { // Used to provide dashjump momentum when moving towards the right. if (transform.localScale.x > 0) { velocity.x = dashJumpCoverage; } // Used to provide dashjump momentum when moving towards the left. else if (transform.localScale.x < 0) { velocity.x = -dashJumpCoverage; } // Stops the Player's dashing immediately if they jump without moving left or right. if (Input.GetAxisRaw("HorizontalMove") == 0) { velocity.x = 0; } } // Starts the timer when Dashing. if (Input.GetButtonDown("Dash")) { // Starts the timeHeld counter for dash calculations. timeHeld = 0f; } // Lets the player starts Dashing once more when the button is released. if (Input.GetButtonUp("Dash")) { // Allows the Player to dash at a new direction once the button is released. flipped = false; } // For the main Dashing code. if (Input.GetButton("Dash")) { // Tracks the amount of time the Player holds the button down. timeHeld += Time.deltaTime; // Controls the timeHeld counter by using timeForFullDash as the limiter. if (timeHeld >= timeForFullDash) { timeHeld = timeForFullDash; } // Formula used to calculate the Player's various dash movements. float horizontalDashForce = ((maxDashForce - minDashForce) * (timeHeld / timeForFullDash)) + minDashForce; float dashCoverage = horizontalDashForce * dashDistance; // If no change of direction occurs during the dash, dash formula will continue. if (flipped == false) { // Used to provide ground dash velocity when moving/facing towards the right. if (controller.collisions.below && transform.localScale.x > 0) { velocity.x = dashCoverage; } // Used to provide ground dash velocity when moving/facing towards the left. if (controller.collisions.below && transform.localScale.x < 0) { velocity.x = -dashCoverage; } } // Ends the dash with normal velocity as the timeHeld counter reaches timeForFullDash. if ((timeHeld == timeForFullDash) && controller.collisions.below) { // Returns Player back to normal velocity and cancels any more dash momentum. velocity.x = standardMovementVelocity; } } } // Perform a check to see if the Player hasn't been knockback yet. else if (playerKnockbackStatus.wasKnockedBack == true) { // If so check to see what direction the Player is currently facing when knockedback. if (playerKnockbackStatus.lockingPlayerScale > 0) { // If so, then forcibly push the Player towards the left when damaged. velocity = new Vector2(-knockbackSpeed, knockbackSpeed / 2.5f); } // If the Player was facing the left direction when knockedback... else if (playerKnockbackStatus.lockingPlayerScale < 0) { // Then forcibly push the Player towards the right when damaged. velocity = new Vector2(knockbackSpeed, knockbackSpeed / 2.5f); } } /* * ////////////////////////////////////////////////////////////////////////////////////// * ////////////////////////////////////////////////////////////////////////////////////// * // Code for air dashing, still needs work. * if (Input.GetKey(KeyCode.X) && !controller.collisions.below && (totalAirDashes < 1)) { * if (transform.localScale.x > 0) { * velocity.x = airDashVelocity; * } * * else { * velocity.x = -airDashVelocity; * } * * // Adds a counter to the air dash total. * totalAirDashes++; * Debug.Log(velocity.x); * // Debug.Log(transform.localPosition); * ////////////////////////////////////////////////////////////////////////////////////// * ////////////////////////////////////////////////////////////////////////////////////// */ // Affects the game's gravity and movement inputs. velocity.y += gravity * Time.deltaTime; controller.Move(velocity * Time.fixedDeltaTime, input); if (controller.collisions.above || controller.collisions.below) { // Sets the Player's gravity to 0 if they're on the ground or against the ceiling. velocity.y = 0; } if (controller.collisions.below) { // Brings back the opportunity to dashjumping again to the Player. lockingAllDashJumping = 0; } }
void DetectClinging() { Vector2 clingNormal = controller.collisions.normal; if (controller.collisions.isAirborne() && !clinging) { clingNormal = Vector2.zero; } float clingAngle = Vector2.Angle(clingNormal, Vector2.up); bool upCling, upLeftCling, upRightCling, leftCling, rightCling; upCling = upLeftCling = upRightCling = leftCling = rightCling = false; if (clingAngle == 180f) { upCling = true; if (facingLeft) { playerSprite.sprite = upClingLeftSprite; } if (facingRight) { playerSprite.sprite = upClingRightSprite; } } if (clingAngle == 135f) { if (clingNormal.x < 0) { upRightCling = true; playerSprite.sprite = upRightClingSprite; } if (clingNormal.x > 0) { upLeftCling = true; playerSprite.sprite = upLeftClingSprite; } } if (clingAngle > 88f && clingAngle < 92f) { if (clingNormal.x < 0) { rightCling = true; playerSprite.sprite = rightClingSprite; } if (clingNormal.x > 0) { leftCling = true; playerSprite.sprite = leftClingSprite; } } if (clingNormal.x < 0) { print("clingNormal.x = " + clingNormal.x); } if (!inPushBack && (upCling || upLeftCling || upRightCling || leftCling || rightCling)) { clinging = true; } if (clinging) { clingFrameCount++; } print("cling angle = " + clingAngle); if (clinging && player.GetButtonDown("jump")) { divekicked = false; canPlayClingSound = true; velocity = clingNormal; if (upCling) { velocity.y = jumpPower * clingNormal.y / 2; } if (upRightCling || upLeftCling) { velocity.x = jumpPower * clingNormal.x / 2; velocity.y = jumpPower * clingNormal.y / 2; } if (rightCling) { facingLeft = true; facingRight = false; velocity.x = jumpPower * clingNormal.x / 2; velocity.y = jumpPower; } if (leftCling) { facingLeft = false; facingRight = true; velocity.x = jumpPower * clingNormal.x / 2; velocity.y = jumpPower; } clinging = upCling = upLeftCling = upRightCling = leftCling = rightCling = false; if (facingLeft) { playerSprite.sprite = jumpFallLeft; } if (facingRight) { playerSprite.sprite = jumpFallRight; } } if (clinging && player.GetButtonDown("attack")) { canPlayClingSound = true; if (upRightCling || rightCling) { facingLeft = true; facingRight = false; divekicked = true; } if (upLeftCling || leftCling) { facingLeft = false; facingRight = true; divekicked = true; } if (upCling) { divekicked = true; } clinging = upCling = upLeftCling = upRightCling = leftCling = rightCling = false; if (facingLeft) { playerSprite.sprite = divekickLeftSprite; } if (facingRight) { playerSprite.sprite = divekickRightSprite; } } if (clinging && clingFrameCount >= clingActiveFrames) { } if (clinging) { print("clinging"); velocity.x = 0; velocity.y = 0; controller.Move(velocity * Time.deltaTime); if (canPlayClingSound) { clingSound.Play(); } canPlayClingSound = false; if (inPushBack || wallSplat) { clinging = false; } } if (controller.collisions.below && canPlayClingSound) { clingSound.Play(); canPlayClingSound = false; } if (controller.collisions.isAirborne() && !clinging) { canPlayClingSound = true; } if (clinging) { divekicked = false; slideAttacked = false; } }
private void Move() { if (controller.collisions.above || controller.collisions.below) { velocity.y = 0; //stops accumulation of gravity } switch (enemytype) { case Enemytype.spider: // do spider stuff here actualTimer -= Time.deltaTime; if (actualTimer <= 0 && controller.collisions.below) { velocity.y = maxJumpVelocity; anim.SetTrigger("jump"); actualTimer = Timer; } if (canmove) //if you can move { anim.SetBool("is run", true); Vector3 thispostion = transform.position; Vector3 otherpostion = target.position; Vector3 direction = otherpostion - thispostion; direction.Normalize(); velocity.x = direction.x * moveSpeed; } else if (!canmove) { anim.SetBool("is run", false); } if (velocity.x <= 0) { sprite.flipX = false; } else { sprite.flipX = true; } Debug.Log(velocity.x); break; case Enemytype.skeleton: // do skeleton stuff here // if move right bool is true means he will move to the right if (MoveRight) { moveX = -1; } else { moveX = 1; } if (velocity.x <= 0) { sprite.flipX = true; } else { sprite.flipX = false; } break; case Enemytype.bat: // do bat stuff here if (batTarget != null) { gravity = batGravity; float obstacalDistance = 999999999f; float playerDistance = 999999999f; foreach (LayerMask mask in controller.collisionMask) { RaycastHit2D hit = Physics2D.Raycast(transform.position, batDirection, detectioncollider.bounds.extents.x, mask); if (hit) { if (hit.transform.gameObject.layer == LayerMask.NameToLayer("Obstacles")) { Debug.DrawRay(transform.position, batDirection * detectioncollider.bounds.extents.x, Color.red); //canmove = false; obstacalDistance = hit.distance; } if (hit.transform.gameObject.layer == LayerMask.NameToLayer("Player")) { Debug.DrawRay(transform.position, batDirection * detectioncollider.bounds.extents.x, Color.red); //canmove = false; playerDistance = hit.distance; } } } if (obstacalDistance < playerDistance) { canmove = false; } if (canmove == true) { batTargetPos = target.position; } else if (canmove == false) { batTargetPos = batTarget.position; } batDirection = batTargetPos - transform.position; batDirection.Normalize(); velocity.x = batDirection.x * moveSpeed; velocity.y = batDirection.y * moveSpeed; } break; } Vector2 input = new Vector2(moveX, 0); // -1 left +1 right float targetVelocityX = input.x * moveSpeed; velocity.x = Mathf.SmoothDamp(velocity.x, targetVelocityX, ref velocitySmoothing, (controller.collisions.below) ? accelTimeGround : accelTimeAir); velocity.y += gravity * Time.deltaTime; //movement and acceleration controller.Move(velocity * Time.deltaTime); }
private void Update() { //Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); Vector2 input = new Vector2(playerActions.Move.X, playerActions.Move.Y); //print(input.ToString("f4")); int wallDirX = (controller.collisions.left) ? -1 : 1; float targetVelocityX = input.x * moveSpeed; float accelerationTime = controller.collisions.below ? accelerationTimeGrounded : accelerationTimeAirborne; velocity.x = Mathf.SmoothDamp(velocity.x, targetVelocityX, ref velocityXSmoothing, accelerationTime); bool wallSliding = false; if ((controller.collisions.left || controller.collisions.right) && !controller.collisions.below && velocity.y < 0) { wallSliding = true; if (velocity.y <= -wallSlideSpeedMax) { velocity.y = -wallSlideSpeedMax; } if (timeToWallUnstick > 0) { velocityXSmoothing = 0; velocity.x = 0; if (input.x != wallDirX && input.x != 0) { timeToWallUnstick -= Time.deltaTime; } else { timeToWallUnstick = wallStickTime; } } else { timeToWallUnstick = wallStickTime; } } if (Input.GetKeyDown(KeyCode.Space)) { if (wallSliding) { if (wallDirX == input.x) { velocity.x = -wallDirX * wallJumpClimb.x; velocity.y = wallJumpClimb.y; } else if (input.x == 0) { velocity.x = -wallDirX * wallJumpOff.x; velocity.y = wallJumpOff.y; } else { velocity.x = -wallDirX * wallLeap.x; velocity.y = wallLeap.y; } } if (controller.collisions.below) { velocity.y = maxJumpVelocity; } } if (Input.GetKeyUp(KeyCode.Space)) { if (velocity.y > minJumpVelocity) { velocity.y = minJumpVelocity; } } velocity.y += gravity * Time.deltaTime; controller.Move(velocity * Time.deltaTime, input); if (controller.collisions.above || controller.collisions.below) { velocity.y = 0; } }
void Update() { Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); horizontalMove = Input.GetAxisRaw("Horizontal") * moveSpeed; animator.SetFloat("MoveSpeed", horizontalMove); int wallDirectionX = (controller.collisions.left) ? -1 : 1; bool wallSliding = false; if ((controller.collisions.left || controller.collisions.right) && !controller.collisions.below && velocity.y < 0) { wallSliding = true; if (velocity.y < -wallSlideSpeedMax) { velocity.y = -wallSlideSpeedMax; } } //This section is the set up for collision detection for both horizontal and vertical axis if (controller.collisions.above || controller.collisions.below) { velocity.y = 0; } if (Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.W)) { animator.SetBool("IsJumping", true); if (wallSliding) { if (wallDirectionX == input.x) { velocity.x = -wallDirectionX * wallJumpClimb.x; velocity.y = wallJumpClimb.y; } else if (input.x == 0) { velocity.x = -wallDirectionX * wallJumpOff.x; velocity.y = wallJumpOff.y; } else { velocity.x = -wallDirectionX * wallJumpLeap.x; velocity.y = wallJumpLeap.y; } } if (controller.collisions.below) { velocity.y = jumpVelocity; } } //Check if there is a collider below the player and that there is no fall/jump velocity if (controller.collisions.below && velocity.y == 0) { OnLanding(); } //float t is used for readability float t = (controller.collisions.below) ? accelerationTimeGrounded : accelerationTimeAirborne; //This part is used for making all transactions smooth so there is no gittering/ glitching out. float targetVelocityX = input.x * moveSpeed; velocity.x = Mathf.SmoothDamp(velocity.x, targetVelocityX, ref velocityXSmoothing, t); velocity.y += gravity * Time.deltaTime; controller.Move(velocity * Time.deltaTime); }
// Update is called once per frame void Update() { // get player input for direction and which direction they are colliding inot the wall Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); int wallDirX = (controller.collisions.left) ? -1 : 1; // currently not sliding on wall bool wallSliding = false; // check if sliding on wall if ((controller.collisions.left || controller.collisions.right) && !controller.collisions.below && velocity.y < 0) { // currently colliding into wall wallSliding = true; // slide down wall slowly if (velocity.y < -wallSlideSpeedMax) { velocity.y = -wallSlideSpeedMax; } } anim.SetInteger("P2Jump", jumping); anim.SetInteger("P2Pickup", pickup); // move if currently selected if (P1ControlScript.P1Selected == false) { // end P1 movement PlayerOne.anim.SetTrigger("P1 Idle"); cameraGuy.GetComponent <CameraController>().player = 2; // stop when hitting object from above or below if (controller.collisions.above || controller.collisions.below) { velocity.y = 0; } if (Input.GetKeyDown(punch) && pickup == 0) { Invoke("emptyPickup", 0.1f); pickup = 1; if (carrying == 0) { GameObject myBullet = (GameObject)Instantiate(Resources.Load("Prefabs/GrabHitbox")); if (facingRight) { myBullet.GetComponent <GrabHitboxScript>().right = 1; myBullet.transform.position = new Vector3(transform.position.x + 1, transform.position.y, transform.position.z); } else { myBullet.GetComponent <GrabHitboxScript>().right = 0; myBullet.transform.position = new Vector3(transform.position.x - 1, transform.position.y, transform.position.z); } } else { GameObject myBullet = (GameObject)Instantiate(Resources.Load("Prefabs/ThrowHitbox")); if (facingRight) { myBullet.GetComponent <GrabHitboxScript>().right = 1; myBullet.transform.position = new Vector3(transform.position.x, transform.position.y + 2, transform.position.z); } else { myBullet.GetComponent <GrabHitboxScript>().right = 0; myBullet.transform.position = new Vector3(transform.position.x, transform.position.y + 2, transform.position.z); } } } // player wants to move left if (Input.GetKey(moveLeft)) { // set right walk cycle if (facingRight) { Flip(); } // set animation and which way the character is facing if (jumping == 0) { anim.SetTrigger("P2 Walking"); } facingRight = false; // move player at normal speed input.x = -moveSpeed; } // player want to move right else if (Input.GetKey(moveRight)) { // set right walk cycle if (!facingRight) { Flip(); } // set animation and which way the character is facing if (jumping == 0) { anim.SetTrigger("P2 Walking"); } facingRight = true; // move player at normal speed input.x = moveSpeed; } // player does not want to move else { // do not move player input.x = 0; // end any movement animation if (jumping == 0) { anim.SetTrigger("P2 Idle"); } } // player wants to jump if (Input.GetKey(jump)) { // check if sliding on wall if (wallSliding) { jumping = 1; floorChecking = 0; Invoke("enableFloorCheck", 0.1f); audioGuy.GetComponent <AudioManager>().Play("Jump"); // player jumps to same wall they are sliding on if (wallDirX == input.x) { velocity.x = -wallDirX * wallJumpClimb.x; velocity.y = wallJumpClimb.y; } // player jumps off wall else if (input.x == 0) { velocity.x = -wallDirX * wallJumpOff.x; velocity.y = wallJumpOff.y; } // player jumps off wall and goes in opposite direction of wall else { velocity.x = -wallDirX * wallLeap.x; velocity.y = wallLeap.y; } } // be able to jump when on ground if (controller.collisions.below) { jumping = 1; floorChecking = 0; Invoke("enableFloorCheck", 0.5f); audioGuy.GetComponent <AudioManager>().Play("Jump"); velocity.y = jumpVelocity; } } } if (controller.collisions.below && floorChecking == 1 && jumping == 1) { jumping = 0; } // check if player is not selected if (P1ControlScript.P1Selected == true) { // do not move player input.x = 0; } // move player float targetVelocityX = input.x * moveSpeed * Time.deltaTime; velocity.x = Mathf.SmoothDamp(velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below) ? accelerationTimeGrounded : accelationTimeAirborne); velocity.y += gravity * Time.deltaTime; controller.Move(velocity * Time.deltaTime); }
// Update is called once per frame void Update() { Vector2 directionalInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); SetDirectionalInput(directionalInput); CalculateVelocity(); HandleWallSliding(); controller.Move(velocity * Time.deltaTime, directionalInput); jumpPressedRemember -= Time.deltaTime; //direction detection if (directionalInput.x < 0) { transform.eulerAngles = new Vector3(0, 0, 0); isFacingRight = false; //ForCamera } else if (directionalInput.x > 0) { transform.eulerAngles = new Vector3(0, 0, 0); isFacingRight = true; //ForCamera } //Gravity + OnGround? if (controller.collisions.below) { isGrounded = true; velocity.y = 0; } else if (controller.collisions.above) { velocity.y = 0; } /* //HorizontalVelocity 'Acceleretion' if needed. * float fHorizontalVelocity = velocity.x; * fHorizontalVelocity += Input.GetAxisRaw("Horizontal"); * if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) < 0.01f) * { * fHorizontalVelocity *= Mathf.Pow(1f - fHorizontalDampingWhenStopping, Time.deltaTime * 10f); * * } * else if (Mathf.Sign(Input.GetAxisRaw("Horizontal")) != Mathf.Sign(fHorizontalVelocity)) * { * fHorizontalVelocity *= Mathf.Pow(1f - fHorizontalDampingWhenTurnning, Time.deltaTime * 10f); * } * else * { * fHorizontalVelocity *= Mathf.Pow(1f - fHorizontalDampingBasic, Time.deltaTime * 10f); * } * * velocity = new Vector2(fHorizontalVelocity, velocity.y); */ if (isGrounded) //Player is on the ground { isJumping = false; doubleJumped = false; canStomp = true; isStomped = false; if (Input.GetButtonDown("Jump")) { velocity.y = maxJumpVelocity; isJumping = true; isWallRunning = true; isGrounded = false; canDoubleJump = true; } } else //Player is in the air { jumpPressedRemember -= Time.deltaTime; if (Input.GetButtonUp("Jump")) //Half Jump { if (velocity.y > minJumpVelocity) { velocity.y = minJumpVelocity; } } if (Input.GetButtonDown("Jump")) //Double jump { if (canDoubleJump) { if (!doubleJumped) { velocity.y = maxJumpVelocity; doubleJumped = true; canDoubleJump = false; } } if (canWallDoubleJump) //Need to fix walldoublejump issue { if (!wallDoubleJumped) { velocity.y = maxJumpVelocity; wallDoubleJumped = true; canWallDoubleJump = false; } } } if (Input.GetKeyDown(KeyCode.C)) // Air Stomp { if (canStomp) { velocity.y = stompSpeed * gravity; canStomp = false; isStomped = true; _shake.CamShake(); } } } }
private void CheckInput() { Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); int wallDirX = (controller.collisions.left) ? -1 : 1; playerScreenPoint = Camera.main.WorldToScreenPoint(mainPlayer.transform.position); if (Input.mousePosition.x > playerScreenPoint.x) { characterModel.flipX = false; gunModel.flipY = false; photonView.RPC("OnSpriteFlipFalse", PhotonTargets.Others); } else { characterModel.flipX = true; gunModel.flipY = true; photonView.RPC("OnSpriteFlipTrue", PhotonTargets.Others); } float targetVelocityX = input.x * moveSpeed; velocity.x = Mathf.SmoothDamp(velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below) ? accelerationTimeGrounded : accelerationTimeAirborne); bool wallSliding = false; if ((controller.collisions.left || controller.collisions.right) && !controller.collisions.below && velocity.y < 0) { wallSliding = true; if (velocity.y < -wallSlideSpeedMax) { velocity.y = -wallSlideSpeedMax; } if (timeToWallUnstick > 0) { velocityXSmoothing = 0; velocity.x = 0; if (input.x != wallDirX && input.x != 0) { timeToWallUnstick -= Time.deltaTime; } else { timeToWallUnstick = wallStickTime; } } else { timeToWallUnstick = wallStickTime; } } if (Input.GetKeyDown(KeyCode.Space)) { if (wallSliding) { if (wallDirX == input.x) { velocity.x = -wallDirX * wallJumpClimb.x; velocity.y = wallJumpClimb.y; } else if (input.x == 0) { velocity.x = -wallDirX * wallJumpOff.x; velocity.y = wallJumpOff.y; } else { velocity.x = -wallDirX * wallLeap.x; velocity.y = wallLeap.y; } } if (controller.collisions.below) { velocity.y = maxJumpVelocity; } } if (Input.GetKeyUp(KeyCode.Space)) { if (velocity.y > minJumpVelocity) { velocity.y = minJumpVelocity; } } if (Input.GetKeyDown(KeyCode.Mouse0)) { Shooting(); } velocity.y += gravity * Time.deltaTime; controller.Move(velocity * Time.deltaTime, input); if (controller.collisions.above || controller.collisions.below) { velocity.y = 0; } }
//Update every frame of player movement void Update() { Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); int wallDirX = (controller.collisions.left) ? -1 : 1; float targetVelocityX = input.x * moveSpeed; velocity.x = Mathf.SmoothDamp(velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below)?accelerationTimeGrounded:accelerationTimeAirbone); bool wallSliding = false; //when player hits a wall using left side or right side, collisions happen and activate the wall riding features if ((controller.collisions.left || controller.collisions.right) && !controller.collisions.below && velocity.y < 0) { wallSliding = true; if (velocity.y < -wallSlideSpeedMax) { velocity.y = -wallSlideSpeedMax; } if (timeToWallUnstick > 0) { velocityXSmoothing = 0; velocity.x = 0; if (input.x != wallDirX && input.x != 0) { timeToWallUnstick -= Time.deltaTime; } else { timeToWallUnstick = wallStickTime; } } else { timeToWallUnstick = wallStickTime; } } //when a spacebar is pressed, if ther isd velocity on player movenment, player ascend; when there is no velocity, player jumps off; if (Input.GetKeyDown(KeyCode.Space)) { if (wallSliding) { if (wallDirX == input.x) { velocity.x = -wallDirX * wallJumpClimb.x; velocity.y = wallJumpClimb.y; } else if (input.x == 0) { velocity.x = -wallDirX * wallJumpOff.x; velocity.y = wallJumpOff.y; } else { velocity.x = -wallDirX * wallLeap.x; velocity.y = wallLeap.y; } } if (controller.collisions.below) { velocity.y = maxJumpVelocity; } } //to set the jump height of the player, when player press longer , they jump higher if (Input.GetKeyUp(KeyCode.Space)) { if (velocity.y > minJumpVelocity) { velocity.y = minJumpVelocity; } } velocity.y += gravity * Time.deltaTime; controller.Move(velocity * Time.deltaTime, input); //player detects collisions from above or below if (controller.collisions.above || controller.collisions.below) { velocity.y = 0; } //UI desgin, when 'c' is pressed, activate narrative intro if (Input.GetKeyDown(KeyCode.C) && count == 0) { countText.text = "Hello there, little data!"; showText.text = "Press C to continue"; } if (Input.GetKeyDown(KeyCode.C) && count == 1) { countText.text = "Welcome to the world of digital."; showText.text = "Press C to continue"; } if (Input.GetKeyDown(KeyCode.C) && count == 2) { countText.text = "Oh No! The 'User' is going to whipe the data!"; showText.text = "Press C to continue"; } if (Input.GetKeyDown(KeyCode.C) && count == 3) { countText.text = "Quick! Run! Run!"; showText.text = "----------------->"; Destroy(loadingScreen, .5f); Destroy(userScreen, 1f); Destroy(objectiveScreen, 1f); } if (Input.GetKeyDown(KeyCode.C)) { count++; } //If 'Esc' is pressed, pause menu is activated if (Input.GetKeyDown(KeyCode.Escape)) { paused = !paused; exitCount++; } //Inside pasue menu, if 'X; is pressed, quit the game if (Input.GetKeyDown(KeyCode.X) && exitCount == 1) { Application.Quit(); exitCount = 0; } //Insde the pause menu, if 'Esc' is press again, game resume if (paused) { pauseScreen.SetActive(true); exitScreen.SetActive(true); Time.timeScale = 0; } if (!paused) { pauseScreen.SetActive(false); exitScreen.SetActive(false); Time.timeScale = 1; } }
// Update is called once per frame void Update() { // If we are colliding on the Y axis we don't want to accumulate gravity, // otherwise when we will fall, acumulated gravity will make the player disappear instead of falling // because it will be too fast, so when we are colliding above or below, we reset Y velocity if (controller.collisions.above || controller.collisions.below) { velocity.y = 0; } if (isActive) { if (isHuman) { if (!controller.collisions.below && !jumping) { hasToLand = true; } if (controller.collisions.below && hasToLand) { PlaySound(clip_land); hasToLand = false; } // Getting input Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); if (Input.GetButtonDown("Jump") && controller.collisions.below) { velocity.y = jumpVelocity; } UpdateAnimation(input); float targetVelocityX = input.x * moveSpeed; // We use smoothDamp to gradually reach our top velocity velocity.x = Mathf.SmoothDamp(velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below) ? accelerationTimeGrounded : accelerationTimeAirborne); if (Input.GetButtonDown("ShapeShift")) { if (CanShapeShift) { ShapeShift(); } } } else { velocity.x = 0; velocity.y = 0; if (Input.GetButtonDown("ShapeShift")) // if is not human { if (CanShapeShift) { BackToHuman(); } } } } velocity.y += gravity * Time.deltaTime; controller.Move(velocity * Time.deltaTime); }
void Update() { timescale = Configuration.Instance.Timescale(transform); Vector2 input = new Vector2(getAxis(right, left), getAxis(up, down)); int wallDirX = (controller.collisions.left) ? -1 : 1; float targetVelocityX = input.x * moveSpeed; velocity.x = Mathf.SmoothDamp(velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below)?accelerationTimeGrounded:accelerationTimeAirborne); bool wallSliding = false; if ((controller.collisions.left || controller.collisions.right) && !controller.collisions.below && velocity.y < 0) { wallSliding = true; if (velocity.y < -wallSlideSpeedMax) { velocity.y = -wallSlideSpeedMax; } if (timeToWallUnstick > 0) { velocityXSmoothing = 0; velocity.x = 0; if (input.x != wallDirX && input.x != 0) { timeToWallUnstick -= Time.deltaTime * timescale; } else { timeToWallUnstick = wallStickTime; } } else { timeToWallUnstick = wallStickTime; } } if (Input.GetKeyDown(jump)) { if (wallSliding) { if (wallDirX == input.x) { velocity.x = -wallDirX * wallJumpClimb.x; velocity.y = wallJumpClimb.y; } else if (input.x == 0) { velocity.x = -wallDirX * wallJumpOff.x; velocity.y = wallJumpOff.y; } else { velocity.x = -wallDirX * wallLeap.x; velocity.y = wallLeap.y; } } if (controller.collisions.below) { velocity.y = maxJumpVelocity; } } if (Input.GetKeyUp(jump)) { if (velocity.y > minJumpVelocity) { velocity.y = minJumpVelocity; } } velocity.y += gravity * Time.deltaTime * timescale; controller.Move(velocity * Time.deltaTime * timescale, input); if (controller.collisions.above || controller.collisions.below) { velocity.y = 0; } }
// Update is called once per frame void Update() { //m_animController.SetWasPlayingClip(); CalculateVelocity(); HandleWallSliding(); controller.Move(velocity * Time.deltaTime, directionalInput); // Reset double jump permissions if wallsliding or grounded if (wallSliding || controller.collisions.below) { m_allowDoubleJump = true; } //call here to make sure we're incorporating platform influence on player if (controller.collisions.above || controller.collisions.below) { if (controller.collisions.slidingDownMaxSlope) { velocity.y += controller.collisions.slopeNormal.y * -fallGravity * Time.deltaTime; } else { velocity.y = 0; } } // Handle Sprite Flipping if (controller.collisions.below) { bool flipSprite = (spriteRenderer.flipX ? (velocity.x > 0.01f) : (velocity.x < -0.01f)); if (flipSprite) { spriteRenderer.flipX = !spriteRenderer.flipX; } } else if (wallSliding) { bool flipSprite = spriteRenderer.flipX ? controller.collisions.left : controller.collisions.right; if (flipSprite) { spriteRenderer.flipX = !spriteRenderer.flipX; } } m_movingForwards = (Mathf.Sign(velocity.x) == (spriteRenderer.flipX ? -1 : 1)); bool isWalking = (Mathf.Abs(velocity.x) < minRunSpeed); animator.SetBool("IsWalking", isWalking); animator.SetBool("MovingForwards", m_movingForwards); animator.SetBool("Grounded", controller.collisions.below); animator.SetFloat("VelocityX", velocity.x); //Mathf.Abs(velocity.x) / moveSpeed); animator.SetFloat("MoveX", Mathf.Abs(velocity.x) / moveSpeed); animator.SetFloat("VelocityY", velocity.y); animator.SetFloat("MoveY", Mathf.Abs(velocity.y) * Time.deltaTime); animator.SetBool("WallSliding", wallSliding); animator.SetBool("DoubleJumping", !m_allowDoubleJump); /* if (wallSliding) { * return; * } * //Handle remaining animation cases * else if (IsStandingStill()) { * m_animController.HandleIdle(); * } * else if (IsLanding()) { * m_animController.HandleLanding(); * } * else if (IsInHangTime()) { * m_animController.HandleAirHangTime(); * } * else if (IsRunning()) { * m_animController.HandleRunning(Mathf.Sign(directionalInput.x)); * } * else { * m_animController.HandleIdle(); * } */ }
private void Update() { if (active) { if (!lr.enabled) { lr.enabled = true; } Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); Vector2 pos = new Vector2(transform.position.x, transform.position.y); RaycastHit2D hit = Physics2D.Raycast(transform.position, mousePosition - pos, 5f, collisionMask); if (hit && hit.collider.tag != "noPortal" && hasPortal) { lr.startColor = lr.endColor = shotA ? Color.blue : Color.yellow; lr.startWidth = .03f; lr.SetPosition(0, pos); lr.SetPosition(1, hit.point); if (Input.GetMouseButtonDown(0) && !shotA) { portalA.transform.position = hit.point + hit.normal * .70f; portalA.SetActive(true); normalA = hit.normal; shotA = true; if (gm) { gm.PlaySound(sounds[1]); } } else if (Input.GetMouseButtonDown(0) && shotA) { portalB.transform.position = hit.point + hit.normal * .70f; portalB.SetActive(true); normalB = hit.normal; shotA = false; portalsActive = true; if (gm) { gm.PlaySound(sounds[2]); } } } else if (hasPortal) { lr.startColor = lr.endColor = Color.white; lr.SetPosition(0, pos); lr.SetPosition(1, ((mousePosition - pos).normalized * 5) + pos); } if (Input.GetKeyDown(KeyCode.R) && (portalA.activeSelf || portalB.activeSelf)) { portalA.SetActive(false); portalB.SetActive(false); shotA = false; portalsActive = false; inA = false; inB = false; if (gm) { gm.PlaySound(sounds[3]); } } if (controller.collisions.above || controller.collisions.below) { velocity.y = 0; anim.SetBool("isJumping", false); } if (Input.GetKeyDown(KeyCode.Space) && controller.collisions.below) { velocity.y = jumpVelocity; anim.SetBool("isJumping", true); if (gm) { gm.PlaySound(sounds[0]); } } if (teleportedA) { transform.position = portalB.transform.position; if (normalB.x == 0 && normalB.y != 0) { velocity.y *= -normalB.y; } else if (normalB.y == 0 && normalB.x != 0) { velocity.x *= -normalB.x; } else { velocity *= -normalB; } } if (teleportedB) { transform.position = portalA.transform.position; if (normalA.x == 0 && normalA.y != 0) { velocity.y *= -normalA.y; } else if (normalA.y == 0 && normalA.x != 0) { velocity.x *= -normalA.x; } else { velocity *= -normalA; } } velocity = Vector2.ClampMagnitude(velocity, 25); float targetVelocityX = input.x * moveSpeed; velocity.x = Mathf.SmoothDamp(velocity.x, targetVelocityX, ref velocityXSmoothing, controller.collisions.below ? accelerationTimeGrounded : accelerationTimeAirborne); velocity.y += gravity * Time.deltaTime; controller.Move(velocity * Time.deltaTime); teleportedB = false; teleportedA = false; if (input.x != 0) { Vector3 newScale = new Vector3(Mathf.Sign(velocity.x) * scaleX, transform.localScale.y, transform.localScale.z); transform.localScale = newScale; if (controller.collisions.below) { anim.SetFloat("speed", Mathf.Clamp(Mathf.Abs(velocity.x * input.x), 0, 1f)); anim.SetBool("isWalking", true); } } else { anim.SetBool("isWalking", false); } } else { lr.enabled = false; anim.SetBool("isWalking", false); anim.SetBool("isJumping", false); velocity.x = 0; velocity.y += gravity * Time.deltaTime; controller.Move(velocity * Time.deltaTime); } }
void Update() { onRail = Physics2D.Raycast(transform.position, new Vector2(0, -1), 1, railMask); velocity.y += gravity * Time.deltaTime; if (controller.collisions.below) { if (jump > 0) { velocity.y = onRail ? jumpVelocity : offRailsJumpVelocity; audioSourceEffects.PlayOneShot(mineCartJump, 1); jump = 0; } else { float acc; if (Mathf.Abs(targetMovementSpeed) < Mathf.Abs(velocity.x) || Mathf.Sign(targetMovementSpeed) != Mathf.Sign(velocity.x)) { acc = onRail ? acceleration : offRailsDeceleration; } else { acc = onRail ? acceleration : offRailsAcceleration; } velocity.x = Numbers.Approach(velocity.x, targetMovementSpeed, acc * Time.deltaTime); } } if (jump > 0) { jump -= Time.deltaTime; } controller.Move(velocity * Time.deltaTime); if (controller.collisions.below || controller.collisions.above) { velocity.y = 0; } if (controller.collisions.below && (controller.collisions.left || controller.collisions.right)) { velocity.x = 0; } targetMovementSpeed = 0; if (velocity.x != 0 && controller.collisions.below) { if (onRail) { audioSource.clip = moveOnTracks; } else { audioSource.clip = moveOffTracks; } audioSource.volume = 2.2f * (Mathf.Abs(velocity.x) / (onRail ? maxMovementSpeed : offRailsMaxMovementSpeed)); } else { audioSource.clip = null; } if (audioSource.clip != lastClip) { if (audioSource.clip != null) { audioSource.Play(); } lastClip = audioSource.clip; } }
public virtual void FixedUpdate() { if (GameController.instance.game.state == Game.State.PAUSED) { return; } //dev tool if (Input.GetKeyDown(KeyCode.P)) { InitGravityValues(); } GetCurrentInput(); if (deathFreeze) { if (Time.time > deathFreezeTime) { deathFreeze = false; wasKilled = true; } else { return; } } if (hitFreeze) { if (freezeFrames < hitFreezeLength) { freezeFrames++; } else { freezeFrames = 0; hitFreeze = false; } return; } if (invulnerable) { if (Time.time > invulnerabilityTimer) { EndInvulnerability(); } } CalculateVelocity(); HandleWallSliding(); controller.Move(velocity * Time.fixedDeltaTime, directionalInput, false, isDead); if ((controller.collisions.above || controller.collisions.below)) { // if (controller.collisions.slidingDownMaxSlope) { // velocity.y += controller.collisions.slopeNormal.y * -gravity * Time.fixedDeltaTime; // } else { // velocity.y = 0; // } velocity.y = 0; } if (controller.collisions.below) { jumpsCompleted = 0; hoversCompleted = 0; superJumping = false; if (!teleporting) { teleportsCompleted = 0; } } if (controller.collisions.shouldBounce) { velocity.y = bounceVelocity; controller.collisions.shouldBounce = false; StartBounce(); canHurtHat = true; } else if (controller.collisions.shouldBounceOnBouncyPlatform) { velocity.y = bouncePlatformVelocity; controller.collisions.shouldBounceOnBouncyPlatform = false; StartBounce(); } else if (controller.collisions.shouldBounceOutOfWell) { velocity.y = bounceOutOfWellVelocity; controller.collisions.shouldBounceOutOfWell = false; StartBounce(); } else if (controller.collisions.shouldBounceOnLava) { velocity.y = bounceOffLavaVelocity; velocity.x = (velocity.x <= 1 && velocity.x >= -1) ? ((velocity.x >= 0) ? 10 : -10) : velocity.x; controller.collisions.shouldBounceOnLava = false; StartBounce(); if (hat.isCurrentlyAttached) { hat.BlowBack(new Vector2(-velocity.x, velocity.y / 2), 0.3f); //what % of player blow back is applied to the hat } } }
void Update() { //Velocidade vertical zero se detectar colisão abaixo //Mudar o collision above para caso de pulo if (controller.collisions.above || controller.collisions.below) { velocity.y = 0; } if (controller.collisions.below) { state.jumping = false; } if (state.movable) { input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); } // Pulo alto if (Input.GetKeyDown(KeyCode.Space) && controller.collisions.below) { velocity.y = maxJumpVelocity; } // Pulo curto if (Input.GetKeyUp(KeyCode.Space)) { if (velocity.y > minJumpVelocity) { velocity.y = minJumpVelocity; } } // Pulando if (velocity.y != 0 && !state.climbing) { state.jumping = true; } if (state.spring && state.jumping) { velocity.y = springSpeed; } // Direção do movimento float targetVelocityX = input.x * moveSpeed; if (state.movable) { velocity.x = Mathf.SmoothDamp(velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below) ? accelerationTimeGrounded : accelerationTimeAirborne); } if (state.hooking) { velocity.x = 0; velocity.y = 0; } else { velocity.y += gravity * Time.deltaTime; } if (velocity.x < 0) { sr.flipX = true; state.movingRight = false; } else if (velocity.x > 0) { sr.flipX = false; state.movingRight = true; } if (state.climbing) { velocity.x = velocity.x / 1.1f; Climb(input); controller.Move(velocity * Time.deltaTime, input); } else { controller.Move(velocity * Time.deltaTime, input); } }
void Update() { if (isTeleporting == false) { Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); int wallDirX = (controller.collisions.left) ? -1 : 1; float targetVelocityX = input.x * moveSpeed; velocity.x = Mathf.SmoothDamp(velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below) ? accelerationTimeGrounded : accelerationTimeAirborne); bool wallSliding = false; if ((controller.collisions.left || controller.collisions.right) && !controller.collisions.below && velocity.y < 0) { anim.SetBool("IsSticked", true); wallSliding = true; if (velocity.y < -wallSlideSpeedMax) { velocity.y = -wallSlideSpeedMax; } if (timeToWallUnstick > 0) { velocityXSmoothing = 0; velocity.x = 0; if (input.x != wallDirX && input.x != 0) { timeToWallUnstick -= Time.deltaTime; } else { GetComponent <SpriteRenderer>().flipX = true; timeToWallUnstick = wallStickTime; } } else { timeToWallUnstick = wallStickTime; } if (controller.collisions.left) { GetComponent <SpriteRenderer>().flipX = false; } } else { anim.SetBool("IsSticked", false); } if (controller.collisions.below) { anim.SetBool("IsJumping", false); } if (input.x == -1 && velocity.y == 0) { GetComponent <SpriteRenderer>().flipX = true; anim.SetBool("IsMoving", true); } else if (input.x == 1 && velocity.y == 0) { GetComponent <SpriteRenderer>().flipX = false; anim.SetBool("IsMoving", true); } else { anim.SetBool("IsMoving", false); } if (Input.GetKeyDown(KeyCode.Space)) { if (wallSliding) { if (wallDirX == input.x) { velocity.x = -wallDirX * wallJumpClimb.x; velocity.y = wallJumpClimb.y; } else if (input.x == 0) { velocity.x = -wallDirX * wallJumpOff.x; velocity.y = wallJumpOff.y; } else { velocity.x = -wallDirX * wallLeap.x; velocity.y = wallLeap.y; } } if (controller.collisions.below) { velocity.y = maxJumpVelocity; anim.SetBool("IsJumping", true); } } if (Input.GetKeyUp(KeyCode.Space)) { if (velocity.y > minJumpVelocity) { velocity.y = minJumpVelocity; } } velocity.y += gravity * Time.deltaTime; controller.Move(velocity * Time.deltaTime, input); if (controller.collisions.above || controller.collisions.below) { velocity.y = 0; } } if (CoolDownTimer > 0) { CoolDownTimer -= Time.deltaTime; } if (CoolDownTimer < 0) { CoolDownTimer = 0; } if (Input.GetMouseButton(0)) { tel.GetComponent <SpriteRenderer>().enabled = true; } else { tel.GetComponent <SpriteRenderer>().enabled = false; } if (Input.GetMouseButtonUp(0) && CoolDownTimer == 0) { if (tel.cantTeleportX == false && tel.cantTeleportY == false) { isTeleporting = true; CoolDownTimer = coolDown; anim.SetBool("IsDissapearing", true); Invoke("Teleporting", 0.2f); } tel.GetComponent <SpriteRenderer>().enabled = false; } }
private void ApplyMovement() { controller.Move(velocity * Time.deltaTime); }
// Update is called once per frame void Update() { Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); int wallDirX = (controller.collisions.left) ? -1 : 1; // -1=sliding on wall to left, 1=sliding on wall to right float targetVelocityX = input.x * moveSpeed; velocity.x = Mathf.SmoothDamp(velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below) ? accelerationTimeGrounded : accelerationTimeAirborn); bool wallSliding = false; if ((controller.collisions.left || controller.collisions.right) && !controller.collisions.below && velocity.y < 0) { wallSliding = true; if (velocity.y < -wallSlideSpeedMax) { velocity.y = -wallSlideSpeedMax; } if (timeToWallUnstick > 0) { velocityXSmoothing = 0; velocity.x = 0; if (input.x != wallDirX && input.x != 0) { timeToWallUnstick -= Time.deltaTime; } else { timeToWallUnstick = wallStickTime; } } else { timeToWallUnstick = wallStickTime; } } if (Input.GetKeyDown(KeyCode.Space)) //remove for walljumping && controller.collisions.below) { { if (wallSliding) { //wall climb jump if (wallDirX == input.x) { velocity.x = -wallDirX * wallJumpClimb.x; velocity.y = wallJumpClimb.y; } //jump off wall else if (input.x == 0) { velocity.x = -wallDirX * wallJumpOff.x; velocity.x = wallJumpOff.y; } //leap off wall else { velocity.x = -wallDirX * wallLeap.x; velocity.y = wallLeap.y; } } //normal ground jump if (controller.collisions.below) { velocity.y = maxJumpVelocity; } } //variable jump height if (Input.GetKeyUp(KeyCode.Space)) { if (velocity.y > minJumpVelocity) { velocity.y = minJumpVelocity; } } velocity.y += gravity * Time.deltaTime; controller.Move(velocity * Time.deltaTime, input); //call here to make sure we're incorporating platform influence on player if (controller.collisions.above || controller.collisions.below) { velocity.y = 0; } }
void Update() { if (transform.position.y <= -10) { Camera.main.transform.position = new Vector3(0, 0, -10); } //check if hitting block from above or below if (controller.collisions.above || controller.collisions.below) { velocity.y = 0; } //check if player is hitting 'end' block if (controller.collisions.end) { if (sound_END == false) { GameObject.Find("Main Camera").GetComponent <CameraController>().MoveToWorld(); sound_END = true; canMove = false; MoveFoward = true; goals.Increment("end_level", 1); if (globals.StageLimit[globals.info.stage - 1] < globals.info.level + 1) { globals.info.stage += 1; globals.info.level = 1; } else { globals.info.level += 1; } StartCoroutine(End()); } } //check if player is hitting 'switcher' block if (controller.collisions.switcher) { // } //check if player is hitting instant kill if (controller.collisions.death) { if (globals.info.health > 0) { globals.info.health = 0; globals.Save_Player(); } } //check if player is hitting 'coin' block if (controller.collisions.coin) { if (sound_COIN == false) { sound_COIN = true; globals.info.score += 100; globals.Save_Player(); StartCoroutine(Coin()); } } //check if player is hitting 'enemy' block if (controller.collisions.enemy && !invinsible) { GetHit(); } //check number of health if (globals.info.health <= 0) { canMove = false; if (sound_OVER == false) { globals.info.lives -= 1; sound_OVER = true; if (globals.info.lives > 0) { globals.info.health = 5; startTimer.dead = true; StartCoroutine(GameOverReset(2)); } else { globals.info.score = 0; globals.info.lives = 0; globals.info.health = 0; startTimer.over = true; StartCoroutine(GameOver()); } } if (globals.info.lives <= 0) { } } //check if start timer is done counting if (startTimer.intro == false) { input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); //check if nothing has paused movement if (canMove) { curTime -= Time.deltaTime; if (curTime <= 0) { curTime = 0; } //deploy shield if (Input.GetMouseButtonDown(1)) { shild = (GameObject)Instantiate(shield, rotor.transform.position, Quaternion.identity); shild.transform.SetParent(rotor.transform); shild.transform.rotation = rotor.transform.rotation; } if (Input.GetMouseButtonUp(1)) { Destroy(shild); } //change rotation of gun Vector3 pos = Camera.main.WorldToScreenPoint(transform.position); Vector3 dir = (Input.mousePosition - pos).normalized; float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg; rotor.transform.rotation = Quaternion.AngleAxis(angle - 90, Vector3.forward); if (canShoot) { if (!gunAuto) { if (Input.GetMouseButtonDown(0)) { if (curTime <= 0) { curTime = reloadTime; GameObject bul = (GameObject)Instantiate(bullet, point.transform.position + 2 * transform.forward, rotor.transform.rotation); bul.GetComponent <Rigidbody2D>().AddForce(dir * bul_speed * 10); } } } else { if (Input.GetMouseButton(0)) { if (curTime <= 0) { curTime = reloadTime; GameObject bul = (GameObject)Instantiate(bullet, point.transform.position + 2 * transform.forward, rotor.transform.rotation); bul.GetComponent <Rigidbody2D>().AddForce(dir * bul_speed * 10); } } } } //initiate jump if player is on ground if (Input.GetKeyDown(KeyCode.Space) && controller.collisions.below) { goals.Increment("jump", 1); sound.PlayOneShot(audioJump, globals.op.option_sound); velocity.y = maxJumpVelocity; } if (Input.GetKeyUp(KeyCode.Space) && !controller.collisions.below) { if (velocity.y > minJumpVelocity) { velocity.y = minJumpVelocity; } } //crouch player if on ground if (Input.GetKey(KeyCode.DownArrow) && controller.collisions.below) { //NOTE: disbaled crouching //transform.localScale = new Vector3(1, .5f, 1); if (sound_CROUCH == false) { sound_CROUCH = true; //NOTE: disabled crouching //StartCoroutine(Crouch()); } ReCheck(); } //returns to normal size when chrouch ends if (Input.GetKeyUp(KeyCode.DownArrow)) { sound_CROUCH = false; //NOTE: disabled crouching //transform.Translate(new Vector3(0, .5f, 0)); transform.localScale = new Vector3(1, 1, 1); ReCheck(); } float targetVelocityX = input.x * MoveSpeed; if (Input.GetKey(KeyCode.LeftShift)) { targetVelocityX = input.x * SprintSpeed; } velocity.x = Mathf.SmoothDamp(velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below) ? accelerationTimeGrounded : accelerationTimeAirborne); velocity.y += gravity * Time.deltaTime; controller.Move(velocity * Time.deltaTime); } else { velocity.x = 0; velocity.y += gravity * Time.deltaTime; controller.Move(velocity * Time.deltaTime); } //forced movement forward if (MoveFoward) { velocity.x = MoveSpeed; controller.Move(velocity * Time.deltaTime); } } }
// Update is called once per frame void Update() { //m_animController.SetWasPlayingClip(); CalculateVelocity(); HandleWallSliding(); controller.Move(velocity * Time.deltaTime, directionalInput); //call here to make sure we're incorporating platform influence on player if (controller.collisions.above || controller.collisions.below) { if (controller.collisions.slidingDownMaxSlope) { velocity.y += controller.collisions.slopeNormal.y * -gravity * Time.deltaTime; } else { velocity.y = 0; } } if (controller.collisions.below) { bool flipSprite = (spriteRenderer.flipX ? (velocity.x > 0.01f) : (velocity.x < -0.01f)); if (flipSprite) { spriteRenderer.flipX = !spriteRenderer.flipX; } } else if (wallSliding) { bool flipSprite = spriteRenderer.flipX ? controller.collisions.left : controller.collisions.right; if (flipSprite) { spriteRenderer.flipX = !spriteRenderer.flipX; } } animator.SetBool("Grounded", controller.collisions.below); animator.SetFloat("VelocityX", velocity.x); //Mathf.Abs(velocity.x) / moveSpeed); animator.SetFloat("MoveX", Mathf.Abs(velocity.x) / moveSpeed); animator.SetFloat("VelocityY", velocity.y); animator.SetFloat("MoveY", Mathf.Abs(velocity.y) * Time.deltaTime); animator.SetBool("WallSliding", wallSliding); /* if (wallSliding) { * return; * } * //Handle remaining animation cases * else if (IsStandingStill()) { * m_animController.HandleIdle(); * } * else if (IsLanding()) { * m_animController.HandleLanding(); * } * else if (IsInHangTime()) { * m_animController.HandleAirHangTime(); * } * else if (IsRunning()) { * m_animController.HandleRunning(Mathf.Sign(directionalInput.x)); * } * else { * m_animController.HandleIdle(); * } */ }
// Update is called once per frame void Update() { if (Input.GetKey(reset)) { SceneManager.LoadScene(SceneManager.GetActiveScene().name); } // get player input for direction and which direction they are colliding inot the wall Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); int wallDirX = (controller.collisions.left) ? -1 : 1; // currently not sliding on wall bool wallSliding = false; GameObject myPartner = GameObject.Find("P2"); // switch who is being controlled if (Input.GetKeyDown(selectPlayer) && P1Selected == true) { P1Selected = false; if (fightingBoss == 1) { myPartner.transform.position = new Vector3(transform.position.x, transform.position.y + 1, transform.position.z); transform.position = new Vector3(-1000, 1000, 1000); } } // switch who is being controlled else if (Input.GetKeyDown(selectPlayer) && P1Selected == false) { P1Selected = true; if (fightingBoss == 1) { transform.position = myPartner.transform.position; myPartner.transform.position = new Vector3(-1000, 1000, 1000); } } anim.SetInteger("P1Punch", punching); // move if currently selected if (P1Selected) { // end P2 movement PlayerTwo.anim.SetTrigger("P2 Idle"); cameraGuy.GetComponent <CameraController>().player = 1; // check if sliding on wall if ((controller.collisions.left || controller.collisions.right) && !controller.collisions.below && velocity.y < 0) { // currently colliding into wall wallSliding = true; // slide down wall slowly if (velocity.y < -wallSlideSpeedMax) { velocity.y = -wallSlideSpeedMax; } } // stop when hitting object from above or below if (controller.collisions.above || controller.collisions.below) { velocity.y = 0; } // player wants to punch if (Input.GetKeyDown(punch) && punching == 0 && jumping == 0) { Invoke("emptyPunch", 0.2f); punching = 1; audioGuy.GetComponent <AudioManager>().Play("Punching"); } // player wants to move left if (Input.GetKey(moveLeft)) { // set right walk cycle if (facingRight) { Flip(); } // set animation and which way the character is facing if (jumping == 0) { anim.SetTrigger("P1 Walking"); } else { anim.SetInteger("P1Jump", 1); } facingRight = false; // move player at normal speed input.x = -moveSpeed; } // player want to move right else if (Input.GetKey(moveRight)) { // set right walk cycle if (!facingRight) { Flip(); } // set animation and which way the character is facing if (jumping == 0) { anim.SetTrigger("P1 Walking"); } else { anim.SetInteger("P1Jump", 1); } facingRight = true; // move player at normal speed input.x = moveSpeed; } // player does not want to move else { // do not move player input.x = 0; // end any movement animation if (jumping == 0) { anim.SetTrigger("P1 Idle"); } else { anim.SetInteger("P1Jump", 1); } } // player wants to jump if (Input.GetKeyDown(jump) && punching == 0) { // check if sliding on wall if (wallSliding) { anim.SetInteger("P1Jump", 1); audioGuy.GetComponent <AudioManager>().Play("Jump"); jumping = 1; floorChecking = 0; Invoke("enableFloorCheck", 0.5f); // player jumps to same wall they are sliding on if (wallDirX == input.x) { velocity.x = -wallDirX * wallJumpClimb.x; velocity.y = wallJumpClimb.y; } // player jumps off wall else if (input.x == 0) { velocity.x = -wallDirX * wallJumpOff.x; velocity.y = wallJumpOff.y; } // player jumps off wall and goes in opposite direction of wall else { velocity.x = -wallDirX * wallLeap.x; velocity.y = wallLeap.y; } } // be able to jump when on ground if (controller.collisions.below) { anim.SetInteger("P1Jump", 1); audioGuy.GetComponent <AudioManager>().Play("Jump"); jumping = 1; floorChecking = 0; Invoke("enableFloorCheck", 0.5f); velocity.y = jumpVelocity; } } } if (controller.collisions.below && floorChecking == 1 && jumping == 1) { anim.SetInteger("P1Jump", 0); jumping = 0; } // check if player is not selected if (P1Selected == false) { // do not move player input.x = 0; } // move player float targetVelocityX = input.x * moveSpeed * Time.deltaTime; velocity.x = Mathf.SmoothDamp(velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below) ? accelerationTimeGrounded : accelationTimeAirborne); velocity.y += gravity * Time.deltaTime; controller.Move(velocity * Time.deltaTime); }