/// <summary> /// Update() /// </summary> void Update() { // Getting the input values GetInput(); // Retrieving the correct deceleration time decelTime = (grounded) ? currentMoveVars.groundedTimeToStop : currentMoveVars.airborneTimeToStop; // IF the passive velocity is greater than zero, decrease it from the current velocity calculations if (passiveVelocity != Vector2.zero) { rigid.velocity -= passiveVelocity; } // ============================= // ========== RUNNING ========== // ============================= // IF there is any running input... if (currentAxisX != 0f) { // Calculating the add velocity factor float addVelocityX = currentAxisX * currentMoveVars.maxRunSpeed * (Time.deltaTime / currentMoveVars.timeToMaxSpeed); // Creating the move vector Vector2 moveVector = new Vector2(); // IF the player is grounded, follow the slope axis, otherwise do NOT do that if (grounded) { moveVector = new Vector2(slopeAxis.x * addVelocityX, slopeAxis.y * addVelocityX); } else { moveVector = new Vector2(addVelocityX, 0f); } // IF the velocity and input directions are different OR the new velocity does not exceed the max velocity... float currentDirectionX = rigid.velocity.x / Mathf.Abs(rigid.velocity.x); if (currentDirectionX != currentAxisX || (rigid.velocity + moveVector).magnitude < currentMoveVars.maxRunSpeed) { // Adding the move vector onto the current velocity rigid.velocity += moveVector; } // ELSE IF the current velocity is less than the max... else if (rigid.velocity.magnitude < currentMoveVars.maxRunSpeed) { // Setting the current velocity to the maximum run speed rigid.velocity = rigid.velocity.normalized * currentMoveVars.maxRunSpeed; } } // IF the player is not holding down a button (but is still moving horizontally)... if (currentAxisX == 0f && rigid.velocity.x != 0f) { // IF the player is grounded, decelerate along the slope axis if (grounded) { // Calculate the deceleration vector Vector2 decelVector = (Time.deltaTime / decelTime) * rigid.velocity.normalized * currentMoveVars.maxRunSpeed; // IF the deceleration vector is LESS than the current velocity... if (decelVector.magnitude < rigid.velocity.magnitude) { rigid.velocity -= decelVector; } else { rigid.velocity = new Vector2(0f, 0f); } } // ELSE... (decelerate exclusively on the X axis) else { // Calulate the deceleration vector X value float decelX = (Time.deltaTime / decelTime) * (rigid.velocity.x / Mathf.Abs(rigid.velocity.x)) * currentMoveVars.maxRunSpeed; // IF the deceleration X value is LESS than the current X velocity... if (Mathf.Abs(decelX) < Mathf.Abs(rigid.velocity.x)) { rigid.velocity -= new Vector2(decelX, 0f); } // ELSE... (the deceleration X value is MORE than the current X velocity) else { rigid.velocity = new Vector2(0f, rigid.velocity.y); } } } // IF the current running sound is defined... if (currentMoveVars.runningSound != string.Empty) { // IF there is horizontal input AND the player is grounded AND the running sound is not playing, start playing the sound if (currentAxisX != 0.0f && grounded && !audioMng.IsPlaying(currentMoveVars.runningSound)) { audioMng.PlayAudio(currentMoveVars.runningSound); } // IF (there is no horizontal input OR the player is not grouned) AND the running sound is playing... if ((currentAxisX == 0.0f || !grounded) && audioMng.IsPlaying(currentMoveVars.runningSound)) { audioMng.StopAudio(currentMoveVars.runningSound); } } // ============================= // ========== JUMPING ========== // ============================= // // IF there is jump input AND it's new input AND the player can still jump... if (currentAxisY > 0f && currentAxisY != lastAxisY && (jumpCounter < currentMoveVars.midairJumps + 1)) { // IF the current jumping sound is defined AND the player is grounded, play the basic jump sound if (currentMoveVars.jumpSound != string.Empty && grounded) { audioMng.PlayAudio(currentMoveVars.jumpSound); } // IF the current midair jumping sound is defined AND the player is not grounded... if (currentMoveVars.midairJumpSound != string.Empty && !grounded) { audioMng.PlayAudio(currentMoveVars.midairJumpSound); } // Setting all of the proper jumping variables (velocity, grounded, and jump counter vars) rigid.velocity = new Vector2(rigid.velocity.x, currentMoveVars.jumpVelocity); grounded = false; jumpCounter++; } // IF there is no input jump AND the lack of input just started AND the player is airborne AND the player is still jumping up... if (currentAxisY == 0f && currentAxisY != lastAxisY && !grounded && rigid.velocity.y > 0f) { // Reduce the player's vertical speed by half rigid.velocity *= new Vector2(1f, 0.5f); } // IF the player is not pressing the down button AND the current Y velocity is faster than the designated fall speed... if (currentAxisY > -1f && rigid.velocity.y < currentMoveVars.maxFallSpeed) { // Calculating the vertical deceleration value float decelY = (Time.deltaTime / decelTime) * currentMoveVars.airborneTimeToStop + Physics2D.gravity.y * -1f; // IF the new Y velocity is less than the max fall speed, add the deceleration value to the current Y velocity if (rigid.velocity.y + decelY < currentMoveVars.maxFallSpeed) { rigid.velocity += new Vector2(0f, decelY); } // ELSE... (the new Y velocity is more than the max fall speed, ergo set the Y velocity to the designated fall speed) else { rigid.velocity = new Vector2(rigid.velocity.x, currentMoveVars.maxFallSpeed); } } // ELSE IF the player is pressing the down button AND the current Y velocity is faster than the designated fall speed... else if (currentAxisY == -1f && rigid.velocity.y < currentMoveVars.maxFallSpeed) { // Keeping the Y velocity the same across the Update() calls rigid.velocity = new Vector2(rigid.velocity.x, lastVelocity.y); } // ===================================== // ========== WAVING THE WAND ========== // ===================================== // Setting the Wand spawn location if (currentAxisX != 0 || currentAxisY != 0) { wandSpawnLocation = new Vector2(currentAxisX, currentAxisY); wandSpawnLocation.Normalize(); } // IF the Wand waving input is down AND it's new... if (currentFire1 > 0f && currentFire1 != lastFire1) { // Spawning the Wand spawnedWand = Instantiate(wandPrefab, transform.position + wandSpawnLocation, Quaternion.identity); wandScript = spawnedWand.GetComponent <StaffTrigger>(); } // IF the Wand waving input is up AND it's new... if (currentFire1 == 0f && currentFire1 != lastFire1) { // Destroying the Wand Destroy(spawnedWand, 0f); } // IF the Wand has been spawned... if (spawnedWand != null) { // Moving the Wand to the right place spawnedWand.transform.position = transform.position + wandSpawnLocation; // Rotating the Wand float rotation = Vector3.SignedAngle(Vector3.right, wandSpawnLocation, Vector3.forward); spawnedWand.transform.rotation = Quaternion.identity; spawnedWand.transform.Rotate(Vector3.forward, rotation); // IF the Wand has been hooked... if (wandScript.IsHooked()) { // Add the hook speed to the player's velocity Vector2 modVector = wandSpawnLocation * currentMoveVars.hookSpeed * Time.deltaTime * currentMoveVars.maxRunSpeed; rigid.velocity += modVector; // IF the lantern boost audio is not playing, then start playing it if (!audioMng.IsPlaying("Lantern Boost")) { audioMng.PlayAudio("Lantern Boost"); } } } // ====================================== // ===== RESTARTING FROM CHECKPOINT ===== // ====================================== // IF the player is pressing the restart button AND it's a new input... if (currentFire2 != 0.0f && currentFire2 != lastFire2) { // Killing the player and then respawning them KillPlayer(); StartCoroutine(RespawnPlayer(forceRespawnTime)); } // Adding the passive velocity rigid.velocity += passiveVelocity; // Clamping the velocities float clampedX = Mathf.Clamp(rigid.velocity.x, -currentMoveVars.maxHorizontalVelocity, currentMoveVars.maxHorizontalVelocity); float clampedY = Mathf.Clamp(rigid.velocity.y, -currentMoveVars.maxVerticalVelocity, currentMoveVars.maxVerticalVelocity); rigid.velocity = new Vector2(clampedX, clampedY); // ANIMATING if (currentAxisX > 0f) { animScript.MoveRight(); } else if (currentAxisX < 0f) { animScript.MoveLeft(); } else { animScript.StandStill(); } // Setting the old input SetOldInput(); }
/// <summary> /// Update() /// </summary> void Update() { // Getting the input values float axisX = Input.GetAxisRaw("Horizontal"); float axisY = Input.GetAxisRaw("Vertical"); float fire1 = Input.GetAxisRaw("Fire1"); // IF there is new running input... if (axisX != 0f && axisX != lastAxisX) { runTimeStart = Time.time; } // IF there is new stopping input... if (axisX == 0f && axisX != lastAxisX) { stopTimeStart = Time.time; } // Calculating the deceleration multiplier decelTime = (grounded) ? currentGroundedTimeToStop : currentAirborneTimeToStop; // Running if (axisX != 0f) { // Calculating the add velocity factor float addVelocityX = axisX * currentMaxRunSpeed * (Time.deltaTime / currentTimeToMaxSpeed); // Creating the move vector Vector2 moveVector = new Vector2(); // IF the player is grounded, follow the slope axis, otherwise do NOT do that if (grounded) { moveVector = new Vector2(slopeAxis.x * addVelocityX, slopeAxis.y * addVelocityX); } else { moveVector = new Vector2(addVelocityX, 0f); } // IF the velocity and move vectors are different OR the new velocity does not exceed the max velocity... if (rigid.velocity.x / Mathf.Abs(rigid.velocity.x) != axisX || (rigid.velocity + moveVector).magnitude < currentMaxRunSpeed) { rigid.velocity += moveVector; } // ELSE IF the current velocity is less than the max... else if (rigid.velocity.magnitude < currentMaxRunSpeed) { rigid.velocity = rigid.velocity.normalized * currentMaxRunSpeed; } } // IF the player is not holding down a button (but is still moving)... if (axisX == 0f && rigid.velocity.x != 0f) { // IF the player is grounded, decelerate along the slope axis if (grounded) { // Calculate the deceleration vector Vector2 decelVector = (Time.deltaTime / decelTime) * rigid.velocity.normalized * currentMaxRunSpeed; // IF the deceleration vector is LESS than the current velocity... if (decelVector.magnitude < rigid.velocity.magnitude) { rigid.velocity -= decelVector; } else { rigid.velocity = new Vector2(0f, 0f); } } // ELSE... (decelerate exclusively on the X axis) else { // Calulate the deceleration vector X value float decelX = (Time.deltaTime / decelTime) * (rigid.velocity.x / Mathf.Abs(rigid.velocity.x)) * currentMaxRunSpeed; // IF the deceleration vector X value is LESS than the current X velocity... if (Mathf.Abs(decelX) < Mathf.Abs(rigid.velocity.x)) { rigid.velocity -= new Vector2(decelX, 0f); } else { rigid.velocity = new Vector2(0f, rigid.velocity.y); } } } // Jumping if (axisY > 0f && axisY != lastAxisY && (jumpCounter < currentMidairJumps + 1)) { rigid.velocity = new Vector2(rigid.velocity.x, currentJumpVelocity); grounded = false; jumpCounter++; } if (axisY == 0f && axisY != lastAxisY && !grounded && rigid.velocity.y > 0f) { rigid.velocity *= new Vector2(1f, 0.5f); } // Waving The Wand if (fire1 > 0f && fire1 != lastFire1) { // Spawning the Wand wandSpawnLocation = new Vector2(axisX, axisY); wandSpawnLocation.Normalize(); spawnedWand = Instantiate(wandPrefab, transform.position + wandSpawnLocation, Quaternion.identity); wandScript = spawnedWand.GetComponent <StaffTrigger>(); // Rotating the Wand float rotation = Vector3.SignedAngle(Vector3.right, wandSpawnLocation, Vector3.forward); spawnedWand.transform.Rotate(Vector3.forward, rotation); } if (fire1 == 0f && fire1 != lastFire1) { Destroy(spawnedWand, 0f); } // IF the wand has been spawned... if (spawnedWand != null) { // Moving the Wand to the right place spawnedWand.transform.position = transform.position + wandSpawnLocation; // IF the Wand has been hooked... if (wandScript.IsHooked()) { Vector2 modVector = wandSpawnLocation * currentHookSpeed * Time.deltaTime * currentMaxRunSpeed; rigid.velocity += modVector; } } // ANIMATING if (axisX > 0f) { animScript.MoveRight(); } else if (axisX < 0f) { animScript.MoveLeft(); } else { animScript.StandStill(); } // Setting the "Last Run Time" variables lastAxisX = axisX; lastAxisY = axisY; lastFire1 = fire1; lastVelocity = rigid.velocity; }