// Update is called once per frame void Update () { // Get movement axis if (playerNum == PlayerNum.Player1) { score = scoreManager.player1Score; } else if (playerNum == PlayerNum.Player2) { score = scoreManager.player2Score; } for (int i = 0; i < scoreNums.Length; i++) { if (i == score) { scoreNums[i].gameObject.SetActive(true); } else { scoreNums[i].gameObject.SetActive(false); } } input2D = new Vector2 (Input.GetAxisRaw (horizontalMove), Input.GetAxisRaw (verticalMove)); float targetvelocityX = input2D.x * moveSpeed; velocity.x = Mathf.SmoothDamp (velocity.x, targetvelocityX, ref velocityXSmoothing, (handler.collisions.below) ? accelerationTimeGrounded : accelerationTimeAirborne); // Grounded check if (handler.collisions.above || handler.collisions.below) { velocity.y = 0; } Jump (); Movement (); Reset(); velocity.y += gravity * Time.deltaTime; handler.Move (velocity * Time.deltaTime); }
void Update() { #region MOVEMENT //Movement Input Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); // Get Input from user if (input.x != 0) { faceDirection = input.x; // Stores last direction the player faced. } animator.SetFloat("Speed", Mathf.Abs(input.x)); int wallDirX = (controller.collisions.left) ? -1 : 1; // Direction of the wall we colided with. //Horizontal movement smoothing float targetVelocityX = input.x * moveSpeed; // Desired velocity we want to achieve when moving velocity.x = Mathf.SmoothDamp(velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below) ? accelerationTimeGrounded : accelerationTimeAirborne); //Smooth the movement between initial velocity and desired velosity (acceleration is taken into account) #endregion #region WALL SLIDE bool wallSliding = false; if ((controller.collisions.left || controller.collisions.right) && // If player touching either wall && !controller.collisions.below && velocity.y < 0) // If player is touching the wall while mid air { wallSliding = true; if (velocity.y < -wallSlideSpeedMax && timeUntilFall > 0) { velocity.y = -wallSlideSpeedMax; // If object falls faster than max wall slide speed while timeUntilFall -= Time.deltaTime; } if (timeToWallUnstick > 0) //Time how much time before player can unstick from the wall (0.25 secs) { velocityXSmoothing = 0; velocity.x = 0; if (input.x != wallDirX && input.x != 0) { timeToWallUnstick -= Time.deltaTime; } else { timeToWallUnstick = wallStickTime; } } else { timeToWallUnstick = wallStickTime; } } animator.SetBool("isOnWall", wallSliding); #endregion //If player is on ground just set its velocity to 0 if (controller.collisions.above || controller.collisions.below) { timeUntilFall = fallTime; // if on floor or hit the hed you can slide again velocity.y = 0; } #region JUMP if (Input.GetKeyDown(KeyCode.Space)) { if (wallSliding) { if (input.x == 0) { velocity.x = -wallDirX * wallJumpOff.x; // If we are not moving and jump while wallsliding then jump off the wall velocity.y = wallJumpOff.y; } else if (-wallDirX == input.x) { timeUntilFall = fallTime; // If jumping and sliding on wall and changing direction => you are able to slide again 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; } } #endregion #region DASH if (Input.GetKeyDown(KeyCode.LeftShift)) { if (timeUntilDash <= 0) { timeUntilDash = dashCooldownTime; velocity.x = faceDirection * dashSpeed; animator.SetTrigger("dashed"); } } if (timeUntilDash > 0) { timeUntilDash -= Time.deltaTime; } #endregion #region FLIP if (velocity.x < 0) { renderer.flipX = true; } else if (velocity.x > 0) { renderer.flipX = false; } #endregion velocity.y += gravity * Time.deltaTime; controller.Move(velocity * Time.deltaTime); }