This article will be free until the accompanying video is released.
Part 11 is going to be the first of a few parts in our Metroidvania series that will focus on redesigning some of the codes in this series to make them more robust, and culminate in us having a new, and more stable saving system. In this part, we are going to be recoding and centralising the features in our project that involve time stopping—namely, the time-stop that happens whenever we pause the game, as well as the hit-stop that happens when the player gets hit.
Currently, both features are managed independently, and this can cause unintentional behaviour in the game sometimes. For example, if you happen to pause when an enemy hits you, the game freezes up even after you unpause, and you will need to do another pause and unpause to resume the game, as shown in the video below.
1. The rationale for code redesign
But before we go deeper into the mechanics of why the pausing issue happens, let’s first talk about these newly-planned parts for the Metroidvania series, as well as the rationale behind it.
a. The importance of building modular systems
When building games—or any application—that has a lot of content, it is very important for our code to be made with modularity in mind. This means that, instead of coding for specific scenarios, you will want to create your code in such a way that they can be used in a large number of situations.
The way Unity is coded is a great example of this—the engine is essentially a library of components that you attach to GameObjects, with each component affecting the behaviour of the GameObject it is attached to in different ways.
Each component is also coded to be usable in many different ways, and in many different situations. Take the Rigidbody component for example:
It is a piece of code that primarily handles physics simulation for any GameObject it’s attached to, and it can be used in many different scenarios, such as:
- On a player character, handling realistic falling, jumping, and collision responses.
- On a thrown object, managing ballistic trajectories and bouncing.
- On a ragdoll, simulating life-like body physics for characters that are knocked down.
Coding in such a manner is called modularity, and this style of coding is extremely important when you are working on larger projects. Primarily, this is because coding in a modular manner encourages code reusability, which is key to making a project stable and manageable.
- Think about it like this: If Unity had no Rigidbody component, and you had to code physics behaviour for every character in your game, how much harder would your project be to manage? You would need to debug and test every character’s physics behaviour for bugs, because they all belong to different scripts.
- On the other hand: If every character in the game uses the Rigidbody component to handle its physics behavior, and you found a bug with physics on one of the characters—fixing this on the Rigidbody script would also fix the issue on all other characters, because they all use the same script.
- Over a long period of time, this modular approach makes your codebase more stable and predictable, because every piece of code is used in multiple places, causing any bugs with them to surface—and be fixed—very quickly. These bugfixes and improvements will also automatically improve every other object in your game that uses the Rigidbody.
b. Areas of improvement in our project
The project that we built in the Metroidvania series so far hasn’t always been made with modularity in mind:
i. Scene fading
Just take for example the scene-fading functionality in various parts of our game—it is managed by different scripts in different scenes, and there are multiple scripts responsible for handling it:
And let’s not forget our UIManager
script as well, which handles transition between scenes.
Because of this, we have a lot of viewers on the forums running into issues with using the fader. If we had coded the scene fading functionality such that it was handled within a single component, we would have had a much easier time coding the scene fading and transitioning functionalities across the project.
ii. Time-stopping features
As mentioned in the start of this post, the time-stopping functionalities that we needed across various parts of the game are also managed by multiple scripts, which cause some issues when their functionalities overlap.
So we are going to modularise the pausing functionality in our project, so that we can make it more flexible in handling different use-cases, while still maintaining its stability.
2. Making the GameManager
the time keeper
In Unity, whenever you want to pause the game, you will manipulate a global variable in the engine called Time.timeScale
. This variable controls how quick time passes, and when you reduce the value to 0, time stops and most of the GameObjects in the engine stop advancing in time.
Hence, the perfect mechanism to use when we want to pause our game.
a. The problem with using Time.timeScale
The problem with Time.timeScale
, however, lies in its accessibility. Because any script can use it, it is very common for 2 different scripts to access the value at the same time and cause issues with their own functionalities as a result.
Take for example, the HitStopTime()
function in PlayerController
, which slows time in the game for a specified number of seconds, before resuming:
public void HitStopTime(float _newTimeScale, int _restoreSpeed, float _delay) { restoreTimeSpeed = _restoreSpeed; if (_delay > 0) { StopCoroutine(StartTimeAgain(_delay)); StartCoroutine(StartTimeAgain(_delay)); } else { restoreTime = true; } Time.timeScale = _newTimeScale; }
Because it uses the same variable Time.timeScale
as GameManager
when it is pausing and unpausing the game, pausing at the same time that there is a hit stop can cause issues, because both functionalities are using the same variable for its own purposes in the game.
private void Update() { if(Input.GetKeyDown(KeyCode.P)) { SaveData.Instance.SavePlayerData(); } if(Input.GetKeyDown(KeyCode.Escape) && !gameIsPaused) { pauseMenu.FadeUIIn(fadeTime); Time.timeScale = 0; gameIsPaused = true; } else if (Input.GetKeyDown(KeyCode.Escape) && gameIsPaused) { pauseMenu.FadeUIOut(fadeTime); UnpauseGame(); } } public void UnpauseGame() { Time.timeScale = 1; gameIsPaused = false; }
In cases like that, it is ideal for both functionalities to be able to account for the effects of the other when both are active.
Hence, to make it easier to handle both functionalities simultaneously, it makes more organisational sense for us to move the hit stop functionality from the PlayerController
script, to the GameManager
script.
b. Improving the pause functionality in GameManager
Before we implement the hit stopping, let’s improve the pausing functionality in the GameManager
script. Currently, the code is a little messy, so cleaning it up will make it easily for us to improve on that functionality later on.
GameManager.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class GameManager : MonoBehaviour { public string transitionedFromScene; public Vector2 platformingRespawnPoint; public Vector2 respawnPoint; [SerializeField] Vector2 defaultRespawnPoint; [SerializeField] Bench bench; public GameObject shade; [SerializeField] private FadeUI pauseMenu; [SerializeField] private float fadeTime;public bool gameIsPaused;public bool isPaused; float lastTimeScale = -1; public bool THKDefeated = false; public static GameManager Instance { get; private set; } private void Awake() { SaveData.Instance.Initialize(); if(Instance != null && Instance != this) { Destroy(gameObject); } else { Instance = this; } if(PlayerController.Instance != null) { if(PlayerController.Instance.halfMana) { SaveData.Instance.LoadShadeData(); if(SaveData.Instance.sceneWithShade == SceneManager.GetActiveScene().name || SaveData.Instance.sceneWithShade == "") { Instantiate(shade, SaveData.Instance.shadePos, SaveData.Instance.shadeRot); } } } SaveScene(); DontDestroyOnLoad(gameObject); bench = FindObjectOfType<Bench>(); SaveData.Instance.LoadBossData(); } private void Update() { if(Input.GetKeyDown(KeyCode.P)) { SaveData.Instance.SavePlayerData(); } if(Input.GetKeyDown(KeyCode.Escape)) { Pause(!isPaused); }if(Input.GetKeyDown(KeyCode.Escape) && !gameIsPaused) { pauseMenu.FadeUIIn(fadeTime); Time.timeScale = 0; gameIsPaused = true; } else if (Input.GetKeyDown(KeyCode.Escape) && gameIsPaused) { pauseMenu.FadeUIOut(fadeTime); UnpauseGame(); }}public void UnpauseGame() { Time.timeScale = 1; gameIsPaused = false; }public void Pause(bool b) { if(b) { // Save the timescale we will restore to. if(lastTimeScale < 0) lastTimeScale = Time.timeScale; Time.timeScale = 0f; } else { Time.timeScale = lastTimeScale; lastTimeScale = -1; } pauseMenu.Fade(fadeTime, b); isPaused = b; } public void SaveScene() { string currentSceneName = SceneManager.GetActiveScene().name; SaveData.Instance.sceneNames.Add(currentSceneName); } public void SaveGame() { SaveData.Instance.SavePlayerData(); } public void RespawnPlayer() { SaveData.Instance.LoadBench(); if(SaveData.Instance.benchSceneName != null) //load the bench's scene if it exists. { SceneManager.LoadScene(SaveData.Instance.benchSceneName); } if(SaveData.Instance.benchPos != null) //set the respawn point to the bench's position. { respawnPoint = SaveData.Instance.benchPos; } else { respawnPoint = defaultRespawnPoint; } PlayerController.Instance.transform.position = respawnPoint; StartCoroutine(UIManager.Instance.DeactivateDeathScreen()); PlayerController.Instance.Respawned(); } }
What we are doing with the changes above is creating a public Pause()
function in the GameManager
, so that any other script that wants to request for a pause will now be able to use the GameManager
to do so. Previously, pausing was hardcoded into the Update()
function, which made it impossible to prompt the GameManager
to trigger a pause.
By recoding the Unpause()
function into one that allows us to trigger pausing and unpausing, we ourselves to simplify our Update()
function as well. Previously, the pausing and unpausing functionality was all hardcoded into Update()
. Now, the function just calls Pause(!isPaused)
, which flips the current pause status of the game (e.g. if the game is paused, it will unpause; and vice versa).
Additionally, we also shorten the isPaused
variable from gameIsPaused
. This is just a matter of personal preference, as I did not think it made sense to make the name so long—since the variable is already in GameManager
, it is not a stretch to assume that isPaused
is referring to the state of the game.
Notice also that in the Pause()
function, we manipulate a value known as lastTimeScale
, which is a variable that is newly-added into GameManager
. This is a variable that is used to record the value of Time.timeScale
at the moment the game was paused, so that we will know what time scale to restore the game to later on.
Previously, we simply restored the timeScale
to 1 whenever we resumed the game. While this works fine for our prototype, if we implement time-slowing or time-speeding mechanics into the game in future, pausing and unpausing will cause bugs with these mechanics.
This value will also come into play later in this part, because the hit stop that we are implementing with GameManager
will need to manipulate this variable as well.
c. Updating other scripts using gameIsPaused
Because we updated the gameIsPaused
variable to isPaused
, we will also need to update some of our scripts that refer to the old variable before our code can work:
Enemy.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Enemy : MonoBehaviour { [SerializeField] protected float health; [SerializeField] protected float recoilLength; [SerializeField] protected float recoilFactor; [SerializeField] protected bool isRecoiling = false; [SerializeField] public float speed; [SerializeField] public float damage; [SerializeField] protected GameObject orangeBlood; [SerializeField] AudioClip hurtSound; protected float recoilTimer; [HideInInspector] public Rigidbody2D rb; protected SpriteRenderer sr; public Animator anim; protected AudioSource audioSource; protected enum EnemyStates { //Crawler Crawler_Idle, Crawler_Flip, //Bat Bat_Idle, Bat_Chase, Bat_Stunned, Bat_Death, //Charger Charger_Idle, Charger_Suprised, Charger_Charge, //Shade Shade_Idle, Shade_Chase, Shade_Stunned, Shade_Death, //THK THK_Stage1, THK_Stage2, THK_Stage3, THK_Stage4 } protected EnemyStates currentEnemyState; protected virtual EnemyStates GetCurrentEnemyState { get { return currentEnemyState; } set { if(currentEnemyState != value) { currentEnemyState = value; ChangeCurrentAnimation(); } } } // Start is called before the first frame update protected virtual void Start() { rb = GetComponent<Rigidbody2D>(); sr = GetComponent<SpriteRenderer>(); anim = GetComponent<Animator>(); audioSource = GetComponent<AudioSource>(); } // Update is called once per frame protected virtual void Update() { if (GameManager.Instance.gameIisPaused) return; if(isRecoiling) { if(recoilTimer < recoilLength) { recoilTimer += Time.deltaTime; } else { isRecoiling = false; recoilTimer = 0; } } else { UpdateEnemyStates(); } } public virtual void EnemyGetsHit(float _damageDone, Vector2 _hitDirection, float _hitForce) { health -= _damageDone; if(!isRecoiling) { audioSource.PlayOneShot(hurtSound); GameObject _orangeBlood = Instantiate(orangeBlood, transform.position, Quaternion.identity); Destroy(_orangeBlood, 5.5f); rb.velocity = _hitForce * recoilFactor * _hitDirection; isRecoiling = true; } } protected virtual void OnCollisionStay2D(Collision2D _other) { if(_other.gameObject.CompareTag("Player") && !PlayerController.Instance.pState.invincible && health > 0) { Attack(); if(PlayerController.Instance.pState.alive) { PlayerController.Instance.HitStopTime(0, 5, 0.5f); } } } protected virtual void Death(float _destroyTime) { Destroy(gameObject, _destroyTime); } protected virtual void UpdateEnemyStates() { } protected virtual void ChangeCurrentAnimation() { } protected void ChangeState(EnemyStates _newState) { GetCurrentEnemyState = _newState; } protected virtual void Attack() { PlayerController.Instance.TakeDamage(damage); } }
PlayerController.cs
using System.Collections; using System.Collections.Generic; using UnityEditor.Build; using UnityEngine; public class PlayerController : MonoBehaviour { [Header("Horizontal Movement Settings:")] [SerializeField] private float walkSpeed = 1; //sets the players movement speed on the ground [Space(5)] [Header("Vertical Movement Settings")] [SerializeField] private float jumpForce = 45f; //sets how hight the player can jump private float jumpBufferCounter = 0; //stores the jump button input [SerializeField] private float jumpBufferFrames; //sets the max amount of frames the jump buffer input is stored private float coyoteTimeCounter = 0; //stores the Grounded() bool [SerializeField] private float coyoteTime; ////sets the max amount of frames the Grounded() bool is stored private int airJumpCounter = 0; //keeps track of how many times the player has jumped in the air [SerializeField] private int maxAirJumps; //the max no. of air jumps [SerializeField] private int maxFallingSpeed; //the max no. of air jumps private float gravity; //stores the gravity scale at start [Space(5)] [Header("Wall Jump Settings")] [SerializeField] private float wallSlidingSpeed = 2f; [SerializeField] private Transform wallCheck; [SerializeField] private LayerMask wallLayer; [SerializeField] private float wallJumpingDuration; [SerializeField] private Vector2 wallJumpingPower; float wallJumpingDirection; bool isWallSliding; bool isWallJumping; [Space(5)] [Header("Ground Check Settings:")] [SerializeField] private Transform groundCheckPoint; //point at which ground check happens [SerializeField] private float groundCheckY = 0.2f; //how far down from ground chekc point is Grounded() checked [SerializeField] private float groundCheckX = 0.5f; //how far horizontally from ground chekc point to the edge of the player is [SerializeField] private LayerMask whatIsGround; //sets the ground layer [Space(5)] [Header("Dash Settings")] [SerializeField] private float dashSpeed; //speed of the dash [SerializeField] private float dashTime; //amount of time spent dashing [SerializeField] private float dashCooldown; //amount of time between dashes [SerializeField] GameObject dashEffect; private bool canDash = true, dashed; [Space(5)] [Header("Attack Settings:")] [SerializeField] private Transform SideAttackTransform; //the middle of the side attack area [SerializeField] private Vector2 SideAttackArea; //how large the area of side attack is [SerializeField] private Transform UpAttackTransform; //the middle of the up attack area [SerializeField] private Vector2 UpAttackArea; //how large the area of side attack is [SerializeField] private Transform DownAttackTransform; //the middle of the down attack area [SerializeField] private Vector2 DownAttackArea; //how large the area of down attack is [SerializeField] private LayerMask attackableLayer; //the layer the player can attack and recoil off of [SerializeField] private float timeBetweenAttack; private float timeSinceAttack; [SerializeField] private float damage; //the damage the player does to an enemy [SerializeField] private GameObject slashEffect; //the effect of the slashs bool restoreTime; float restoreTimeSpeed; [Space(5)] [Header("Recoil Settings:")] [SerializeField] private int recoilXSteps = 5; //how many FixedUpdates() the player recoils horizontally for [SerializeField] private int recoilYSteps = 5; //how many FixedUpdates() the player recoils vertically for [SerializeField] private float recoilXSpeed = 100; //the speed of horizontal recoil [SerializeField] private float recoilYSpeed = 100; //the speed of vertical recoil private int stepsXRecoiled, stepsYRecoiled; //the no. of steps recoiled horizontally and verticall [Space(5)] [Header("Health Settings")] public int health; public int maxHealth; public int maxTotalHealth = 10; public int heartShards; [SerializeField] GameObject bloodSpurt; [SerializeField] float hitFlashSpeed; public delegate void OnHealthChangedDelegate(); [HideInInspector] public OnHealthChangedDelegate onHealthChangedCallback; float healTimer; [SerializeField] float timeToHeal; [Space(5)] [Header("Mana Settings")] [SerializeField] UnityEngine.UI.Image manaStorage; [SerializeField] float mana; [SerializeField] float manaDrainSpeed; [SerializeField] float manaGain; public bool halfMana; public ManaOrbsHandler manaOrbsHandler; public int orbShard; public int manaOrbs; [Space(5)] [Header("Spell Settings")] //spell stats [SerializeField] float manaSpellCost = 0.3f; [SerializeField] float timeBetweenCast = 0.5f; [SerializeField] float spellDamage; //upspellexplosion and downspellfireball [SerializeField] float downSpellForce; // desolate dive only //spell cast objects [SerializeField] GameObject sideSpellFireball; [SerializeField] GameObject upSpellExplosion; [SerializeField] GameObject downSpellFireball; float timeSinceCast; float castOrHealTimer; [Space(5)] [Header("Camera Stuff")] [SerializeField] private float playerFallSpeedThreshold = -10; [Space(5)] [Header("Audio")] [SerializeField] AudioClip landingSound; [SerializeField] AudioClip jumpSound; [SerializeField] AudioClip dashAndAttackSound; [SerializeField] AudioClip spellCastSound; [SerializeField] AudioClip hurtSound; [HideInInspector] public PlayerStateList pState; [HideInInspector] public Rigidbody2D rb; private Animator anim; private SpriteRenderer sr; private AudioSource audioSource; //Input Variables private float xAxis, yAxis; private bool attack = false; bool openMap; bool openInventory; private bool canFlash = true; private bool landingSoundPlayed; public static PlayerController Instance; //unlocking public bool unlockedWallJump; public bool unlockedDash; public bool unlockedVarJump; public bool unlockedSideCast; public bool unlockedUpCast; public bool unlockedDownCast; private void Awake() { if (Instance != null && Instance != this) { Destroy(gameObject); } else { Instance = this; } DontDestroyOnLoad(gameObject); } // Start is called before the first frame update void Start() { pState = GetComponent<PlayerStateList>(); rb = GetComponent<Rigidbody2D>(); sr = GetComponent<SpriteRenderer>(); anim = GetComponent<Animator>(); manaOrbsHandler = FindObjectOfType<ManaOrbsHandler>(); audioSource = GetComponent<AudioSource>(); gravity = rb.gravityScale; Mana = mana; manaStorage.fillAmount = Mana; Health = maxHealth; SaveData.Instance.LoadPlayerData(); if (manaOrbs > 3) { manaOrbs = 3; } if (halfMana) { UIManager.Instance.SwitchMana(UIManager.ManaState.HalfMana); } else { UIManager.Instance.SwitchMana(UIManager.ManaState.FullMana); } onHealthChangedCallback.Invoke(); if (Health == 0) { pState.alive = false; GameManager.Instance.RespawnPlayer(); } } private void OnDrawGizmos() { Gizmos.color = Color.red; Gizmos.DrawWireCube(SideAttackTransform.position, SideAttackArea); Gizmos.DrawWireCube(UpAttackTransform.position, UpAttackArea); Gizmos.DrawWireCube(DownAttackTransform.position, DownAttackArea); } // Update is called once per frame void Update() { if (pState.cutscene || GameManager.Instance.gameIisPaused) return; if (pState.alive) { GetInputs(); ToggleMap(); ToggleInventory(); } UpdateJumpVariables(); RestoreTimeScale(); UpdateCameraYDampForPlayerFall(); if (pState.dashing) return; FlashWhileInvincible(); if (!pState.alive) return; if (!isWallJumping) { Move(); } Heal(); CastSpell(); if (pState.healing) return; if (!isWallJumping) { Flip(); Jump(); } if (unlockedWallJump) { WallSlide(); WallJump(); } if (unlockedDash) { StartDash(); } Attack(); } private void OnTriggerEnter2D(Collider2D _other) //for up and down cast spell { if(_other.GetComponent<Enemy>() != null && pState.casting) { _other.GetComponent<Enemy>().EnemyGetsHit(spellDamage, (_other.transform.position - transform.position).normalized, -recoilYSpeed); } } private void FixedUpdate() { if (pState.cutscene) return; if (pState.dashing || pState.healing) return; Recoil(); } void GetInputs() { xAxis = Input.GetAxisRaw("Horizontal"); yAxis = Input.GetAxisRaw("Vertical"); attack = Input.GetButtonDown("Attack"); openMap = Input.GetButton("Map"); openInventory = Input.GetButton("Inventory"); if (Input.GetButton("Cast/Heal")) { castOrHealTimer += Time.deltaTime; } } void ToggleMap() { if(openMap) { UIManager.Instance.mapHandler.SetActive(true); } else { UIManager.Instance.mapHandler.SetActive(false); } } void ToggleInventory() { if (openInventory) { UIManager.Instance.inventory.SetActive(true); } else { UIManager.Instance.inventory.SetActive(false); } } void Flip() { if (xAxis < 0) { transform.eulerAngles = new Vector2(0, 180); pState.lookingRight = false; } else if (xAxis > 0) { transform.eulerAngles = new Vector2(0, 0); pState.lookingRight = true; } } private void Move() { if (pState.healing) rb.velocity = new Vector2(0, 0); rb.velocity = new Vector2(walkSpeed * xAxis, rb.velocity.y); anim.SetBool("Walking", rb.velocity.x != 0 && Grounded()); } void UpdateCameraYDampForPlayerFall() { //if falling past a certain speed threshold if (rb.velocity.y < playerFallSpeedThreshold && !CameraManager.Instance.isLerpingYDamping && !CameraManager.Instance.hasLerpedYDamping) { StartCoroutine(CameraManager.Instance.LerpYDamping(true)); } //if standing stil or moving up if(rb.velocity.y >= 0 && !CameraManager.Instance.isLerpingYDamping && CameraManager.Instance.hasLerpedYDamping) { //reset camera function CameraManager.Instance.hasLerpedYDamping = false; StartCoroutine(CameraManager.Instance.LerpYDamping(false)); } } void StartDash() { if (Input.GetButtonDown("Dash") && canDash && !dashed) { StartCoroutine(Dash()); dashed = true; } if (Grounded()) { dashed = false; } } IEnumerator Dash() { canDash = false; pState.dashing = true; anim.SetTrigger("Dashing"); audioSource.PlayOneShot(dashAndAttackSound); rb.gravityScale = 0; int _dir = pState.lookingRight ? 1 : -1; rb.velocity = new Vector2(_dir * dashSpeed, 0); if (Grounded()) Instantiate(dashEffect, transform); yield return new WaitForSeconds(dashTime); rb.gravityScale = gravity; pState.dashing = false; yield return new WaitForSeconds(dashCooldown); canDash = true; } public IEnumerator WalkIntoNewScene(Vector2 _exitDir, float _delay) { pState.invincible = true; //If exit direction is upwards if (_exitDir.y != 0) { rb.velocity = jumpForce * _exitDir; } //If exit direction requires horizontal movement if (_exitDir.x != 0) { xAxis = _exitDir.x > 0 ? 1 : -1; Move(); } Flip(); yield return new WaitForSeconds(_delay); print("cutscene played"); pState.invincible = false; pState.cutscene = false; } void Attack() { timeSinceAttack += Time.deltaTime; if (attack && timeSinceAttack >= timeBetweenAttack) { timeSinceAttack = 0; anim.SetTrigger("Attacking"); audioSource.PlayOneShot(dashAndAttackSound); if (yAxis == 0 || yAxis < 0 && Grounded()) { int _recoilLeftOrRight = pState.lookingRight ? 1 : -1; Hit(SideAttackTransform, SideAttackArea, ref pState.recoilingX, Vector2.right * _recoilLeftOrRight, recoilXSpeed); Instantiate(slashEffect, SideAttackTransform); } else if (yAxis > 0) { Hit(UpAttackTransform, UpAttackArea, ref pState.recoilingY, Vector2.up, recoilYSpeed); SlashEffectAtAngle(slashEffect, 80, UpAttackTransform); } else if (yAxis < 0 && !Grounded()) { Hit(DownAttackTransform, DownAttackArea, ref pState.recoilingY, Vector2.down, recoilYSpeed); SlashEffectAtAngle(slashEffect, -90, DownAttackTransform); } } } void Hit(Transform _attackTransform, Vector2 _attackArea, ref bool _recoilBool, Vector2 _recoilDir, float _recoilStrength) { Collider2D[] objectsToHit = Physics2D.OverlapBoxAll(_attackTransform.position, _attackArea, 0, attackableLayer); List<Enemy> hitEnemies = new List<Enemy>(); if (objectsToHit.Length > 0) { _recoilBool = true; } for (int i = 0; i < objectsToHit.Length; i++) { Enemy e = objectsToHit[i].GetComponent<Enemy>(); if (e && !hitEnemies.Contains(e)) { e.EnemyGetsHit(damage, _recoilDir, _recoilStrength); hitEnemies.Add(e); if (objectsToHit[i].CompareTag("Enemy")) { if (!halfMana && Mana < 1 || (halfMana && Mana < 0.5)) { Mana += manaGain; } else { manaOrbsHandler.UpdateMana(manaGain * 3); } } } } } void SlashEffectAtAngle(GameObject _slashEffect, int _effectAngle, Transform _attackTransform) { _slashEffect = Instantiate(_slashEffect, _attackTransform); _slashEffect.transform.eulerAngles = new Vector3(0, 0, _effectAngle); _slashEffect.transform.localScale = new Vector2(transform.localScale.x, transform.localScale.y); } void Recoil() { if (pState.recoilingX) { if (pState.lookingRight) { rb.velocity = new Vector2(-recoilXSpeed, 0); } else { rb.velocity = new Vector2(recoilXSpeed, 0); } } if (pState.recoilingY) { rb.gravityScale = 0; if (yAxis < 0) { rb.velocity = new Vector2(rb.velocity.x, recoilYSpeed); } else { rb.velocity = new Vector2(rb.velocity.x, -recoilYSpeed); } airJumpCounter = 0; } else { rb.gravityScale = gravity; } //stop recoil if (pState.recoilingX && stepsXRecoiled < recoilXSteps) { stepsXRecoiled++; } else { StopRecoilX(); } if (pState.recoilingY && stepsYRecoiled < recoilYSteps) { stepsYRecoiled++; } else { StopRecoilY(); } if (Grounded()) { StopRecoilY(); } } void StopRecoilX() { stepsXRecoiled = 0; pState.recoilingX = false; } void StopRecoilY() { stepsYRecoiled = 0; pState.recoilingY = false; } public void TakeDamage(float _damage) { if(pState.alive) { audioSource.PlayOneShot(hurtSound); Health -= Mathf.RoundToInt(_damage); if(Health <= 0) { Health = 0; StartCoroutine(Death()); } else { StartCoroutine(StopTakingDamage()); } } } IEnumerator StopTakingDamage() { pState.invincible = true; GameObject _bloodSpurtParticles = Instantiate(bloodSpurt, transform.position, Quaternion.identity); Destroy(_bloodSpurtParticles, 1.5f); anim.SetTrigger("TakeDamage"); yield return new WaitForSeconds(1f); pState.invincible = false; } IEnumerator Flash() { sr.enabled = !sr.enabled; canFlash = false; yield return new WaitForSeconds(0.1f); canFlash = true; } void FlashWhileInvincible() { if (pState.invincible && !pState.cutscene) { if(Time.timeScale > 0.2 && canFlash) { StartCoroutine(Flash()); } } else { sr.enabled = true; } } void RestoreTimeScale() { if (restoreTime) { if (Time.timeScale < 1) { Time.timeScale += Time.unscaledDeltaTime * restoreTimeSpeed; } else { Time.timeScale = 1; restoreTime = false; } } } public void HitStopTime(float _newTimeScale, int _restoreSpeed, float _delay) { restoreTimeSpeed = _restoreSpeed; if (_delay > 0) { StopCoroutine(StartTimeAgain(_delay)); StartCoroutine(StartTimeAgain(_delay)); } else { restoreTime = true; } Time.timeScale = _newTimeScale; } IEnumerator StartTimeAgain(float _delay) { yield return new WaitForSecondsRealtime(_delay); restoreTime = true; } IEnumerator Death() { pState.alive = false; Time.timeScale = 1f; GameObject _bloodSpurtParticles = Instantiate(bloodSpurt, transform.position, Quaternion.identity); Destroy(_bloodSpurtParticles, 1.5f); anim.SetTrigger("Death"); rb.constraints = RigidbodyConstraints2D.FreezePosition; GetComponent<BoxCollider2D>().enabled = false; yield return new WaitForSeconds(0.9f); StartCoroutine(UIManager.Instance.ActivateDeathScreen()); yield return new WaitForSeconds(0.1f); Instantiate(GameManager.Instance.shade, transform.position, Quaternion.identity); SaveData.Instance.SavePlayerData(); } public void Respawned() { if (!pState.alive) { rb.constraints = RigidbodyConstraints2D.None; rb.constraints = RigidbodyConstraints2D.FreezeRotation; GetComponent<BoxCollider2D>().enabled = true; pState.alive = true; halfMana = true; UIManager.Instance.SwitchMana(UIManager.ManaState.HalfMana); Mana = 0; Health = maxHealth; anim.Play("Player_Idle"); } } public void RestoreMana() { halfMana = false; UIManager.Instance.SwitchMana(UIManager.ManaState.FullMana); } public int Health { get { return health; } set { if (health != value) { health = Mathf.Clamp(value, 0, maxHealth); if (onHealthChangedCallback != null) { onHealthChangedCallback.Invoke(); } } } } void Heal() { if (Input.GetButton("Cast/Heal") && castOrHealTimer > 0.1f && Health < maxHealth && Mana > 0 && Grounded() && !pState.dashing) { pState.healing = true; anim.SetBool("Healing", true); //healing healTimer += Time.deltaTime; if (healTimer >= timeToHeal) { Health++; healTimer = 0; } //drain mana manaOrbsHandler.usedMana = true; manaOrbsHandler.countDown = 3f; Mana -= Time.deltaTime * manaDrainSpeed; } else { pState.healing = false; anim.SetBool("Healing", false); healTimer = 0; } } public float Mana { get { return mana; } set { //if mana stats change if (mana != value) { if(!halfMana) { mana = Mathf.Clamp(value, 0, 1); } else { mana = Mathf.Clamp(value, 0, 0.5f); } manaStorage.fillAmount = Mana; } } } void CastSpell() { if (Input.GetButtonUp("Cast/Heal") && castOrHealTimer <= 0.1f && timeSinceCast >= timeBetweenCast && Mana >= manaSpellCost) { pState.casting = true; timeSinceCast = 0; StartCoroutine(CastCoroutine()); } else { timeSinceCast += Time.deltaTime; } if (!Input.GetButton("Cast/Heal")) { castOrHealTimer = 0; } if (Grounded()) { //disable downspell if on the ground downSpellFireball.SetActive(false); } //if down spell is active, force player down until grounded if(downSpellFireball.activeInHierarchy) { rb.velocity += downSpellForce * Vector2.down; } } IEnumerator CastCoroutine() { //side cast if ((yAxis == 0 || (yAxis < 0 && Grounded())) && unlockedSideCast) { audioSource.PlayOneShot(spellCastSound); anim.SetBool("Casting", true); yield return new WaitForSeconds(0.15f); GameObject _fireBall = Instantiate(sideSpellFireball, SideAttackTransform.position, Quaternion.identity); //flip fireball if(pState.lookingRight) { _fireBall.transform.eulerAngles = Vector3.zero; // if facing right, fireball continues as per normal } else { _fireBall.transform.eulerAngles = new Vector2(_fireBall.transform.eulerAngles.x, 180); //if not facing right, rotate the fireball 180 deg } pState.recoilingX = true; Mana -= manaSpellCost; manaOrbsHandler.usedMana = true; manaOrbsHandler.countDown = 3f; yield return new WaitForSeconds(0.35f); } //up cast else if( yAxis > 0 && unlockedUpCast) { audioSource.PlayOneShot(spellCastSound); anim.SetBool("Casting", true); yield return new WaitForSeconds(0.15f); Instantiate(upSpellExplosion, transform); rb.velocity = Vector2.zero; Mana -= manaSpellCost; manaOrbsHandler.usedMana = true; manaOrbsHandler.countDown = 3f; yield return new WaitForSeconds(0.35f); } //down cast else if((yAxis < 0 && !Grounded()) && unlockedDownCast) { audioSource.PlayOneShot(spellCastSound); anim.SetBool("Casting", true); yield return new WaitForSeconds(0.15f); downSpellFireball.SetActive(true); Mana -= manaSpellCost; manaOrbsHandler.usedMana = true; manaOrbsHandler.countDown = 3f; yield return new WaitForSeconds(0.35f); } anim.SetBool("Casting", false); pState.casting = false; } public bool Grounded() { if (Physics2D.Raycast(groundCheckPoint.position, Vector2.down, groundCheckY, whatIsGround) || Physics2D.Raycast(groundCheckPoint.position + new Vector3(groundCheckX, 0, 0), Vector2.down, groundCheckY, whatIsGround) || Physics2D.Raycast(groundCheckPoint.position + new Vector3(-groundCheckX, 0, 0), Vector2.down, groundCheckY, whatIsGround)) { return true; } else { return false; } } void Jump() { if (jumpBufferCounter > 0 && coyoteTimeCounter > 0 && !pState.jumping) { if (Input.GetButtonDown("Jump")) { audioSource.PlayOneShot(jumpSound); } rb.velocity = new Vector3(rb.velocity.x, jumpForce); pState.jumping = true; } if (!Grounded() && airJumpCounter < maxAirJumps && Input.GetButtonDown("Jump") && unlockedVarJump) { audioSource.PlayOneShot(jumpSound); pState.jumping = true; airJumpCounter++; rb.velocity = new Vector3(rb.velocity.x, jumpForce); } if (Input.GetButtonUp("Jump") && rb.velocity.y > 3) { pState.jumping = false; rb.velocity = new Vector2(rb.velocity.x, 0); } rb.velocity = new Vector2(rb.velocity.x, Mathf.Clamp(rb.velocity.y, -maxFallingSpeed, rb.velocity.y)); anim.SetBool("Jumping", !Grounded()); } void UpdateJumpVariables() { if (Grounded()) { if (!landingSoundPlayed) { audioSource.PlayOneShot(landingSound); landingSoundPlayed = true; } pState.jumping = false; coyoteTimeCounter = coyoteTime; airJumpCounter = 0; } else { coyoteTimeCounter -= Time.deltaTime; landingSoundPlayed = false; } if (Input.GetButtonDown("Jump")) { jumpBufferCounter = jumpBufferFrames; } else { jumpBufferCounter = jumpBufferCounter - Time.deltaTime * 10; } } private bool Walled() { return Physics2D.OverlapCircle(wallCheck.position, 0.2f, wallLayer); } void WallSlide() { if(Walled() && !Grounded() && xAxis != 0) { isWallSliding = true; rb.velocity = new Vector2(rb.velocity.x, Mathf.Clamp(rb.velocity.y, -wallSlidingSpeed, float.MaxValue)); } else { isWallSliding = false; } } void WallJump() { if (isWallSliding) { isWallJumping = false; wallJumpingDirection = !pState.lookingRight ? 1 : -1; CancelInvoke(nameof(StopWallJumping)); } if (Input.GetButtonDown("Jump") && isWallSliding) { audioSource.PlayOneShot(jumpSound); isWallJumping = true; rb.velocity = new Vector2(wallJumpingDirection * wallJumpingPower.x, wallJumpingPower.y); dashed = false; airJumpCounter = 0; pState.lookingRight = !pState.lookingRight; float jumpDirection = pState.lookingRight ? 0 : 180; transform.eulerAngles = new Vector2(transform.eulerAngles.x, jumpDirection); Invoke(nameof(StopWallJumping), wallJumpingDuration); } } void StopWallJumping() { isWallJumping = false; transform.eulerAngles = new Vector2(transform.eulerAngles.x, 0); } }
We will also need to add a new Fade()
function to our FadeUI
, as the newly-edited GameManager
script calls the Fade()
function in FadeUI
, as opposed to calling FadeIn()
or FadeOut()
.
FadeUI.cs
using System.Collections; using UnityEngine; public class FadeUI : MonoBehaviour { CanvasGroup canvasGroup; private void Awake() { canvasGroup = GetComponent<CanvasGroup>(); } public void Fade(float _seconds, bool fadeIn) { if (fadeIn) FadeUIIn(_seconds); else FadeUIOut(_seconds); } public void FadeUIOut(float _seconds) { StartCoroutine(FadeOut(_seconds)); } public void FadeUIIn(float _seconds) { StartCoroutine(FadeIn(_seconds)); } IEnumerator FadeOut(float _seconds) { canvasGroup.interactable = false; canvasGroup.blocksRaycasts = false; canvasGroup.alpha = 1; while(canvasGroup.alpha > 0) { canvasGroup.alpha -= Time.unscaledDeltaTime / _seconds; yield return null; } yield return null; } IEnumerator FadeIn(float _seconds) { canvasGroup.alpha = 0; while(canvasGroup.alpha < 1) { canvasGroup.alpha += Time.unscaledDeltaTime / _seconds; yield return null; } canvasGroup.interactable = true; canvasGroup.blocksRaycasts = true; yield return null; } // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { } }
d. Reassigning the trigger for the Resume button
Once the above is done, you should no longer have code compilation errors. However, you might find that the Pause Screen’s Resume is no longer fully working. It will fade the pause screen away, but no longer resume the game.
This is because we replaced the Unpause()
function in the GameManager
, which it uses. Hence, remember to assign the new Pause()
function to the button, and make sure you uncheck the checkbox (i.e. false
value for Pause()
) so that Unity will register it as an unpause action.
3. Creating a Stop()
function for the GameManager
Now that the pausing functionality is cleaned up, it is time to move HitStopTime()
from PlayerController
to the GameManager
.
a. Adding Stop()
and HandleStopGame()
Let’s first create 2 new functions in GameManager
, Stop()
and HandleStopGame()
, to replace HitStopTime()
:
GameManager.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class GameManager : MonoBehaviour { public string transitionedFromScene; public Vector2 platformingRespawnPoint; public Vector2 respawnPoint; [SerializeField] Vector2 defaultRespawnPoint; [SerializeField] Bench bench; public GameObject shade; [SerializeField] private FadeUI pauseMenu; [SerializeField] private float fadeTime; public bool isPaused; float lastTimeScale = -1f; static Coroutine stopGameCoroutine; public static bool isStopped { get { return stopGameCoroutine != null; } } public bool THKDefeated = false; public static GameManager Instance { get; private set; } private void Awake() { SaveData.Instance.Initialize(); if(Instance != null && Instance != this) { Destroy(gameObject); } else { Instance = this; } if(PlayerController.Instance != null) { if(PlayerController.Instance.halfMana) { SaveData.Instance.LoadShadeData(); if(SaveData.Instance.sceneWithShade == SceneManager.GetActiveScene().name || SaveData.Instance.sceneWithShade == "") { Instantiate(shade, SaveData.Instance.shadePos, SaveData.Instance.shadeRot); } } } SaveScene(); DontDestroyOnLoad(gameObject); bench = FindObjectOfType<Bench>(); SaveData.Instance.LoadBossData(); } private void Update() { if(Input.GetKeyDown(KeyCode.P)) { SaveData.Instance.SavePlayerData(); } if(Input.GetKeyDown(KeyCode.Escape)) { Pause(!isPaused); } } public void Pause(bool b) { if(b) { // Save the timescale we will restore to. if(lastTimeScale < 0) lastTimeScale = Time.timeScale; Time.timeScale = 0f; } else { if(!isStopped) { Time.timeScale = lastTimeScale; lastTimeScale = -1; } } isPaused = b; } public static void Stop(float duration = .5f, float restoreDelay = .1f, float slowMultiplier = 0f) { if (stopGameCoroutine != null) return; stopGameCoroutine = Instance.StartCoroutine(HandleStopGame(duration, restoreDelay, slowMultiplier)); } // Used to create the hit stop effect. // <duration> specifies how long it lasts for. // <restoreDelay> specifies how quickly we go back to the original time scale. // <stopMultiplier> lets us control how much the stop is. static IEnumerator HandleStopGame(float duration, float restoreDelay, float slowMultiplier = 0f) { if(Instance.lastTimeScale < 0) Instance.lastTimeScale = Time.timeScale; // Saves the original time scale for restoration later. Time.timeScale = Instance.lastTimeScale * slowMultiplier; // Counts down every frame until the stop game is finished. WaitForEndOfFrame w = new WaitForEndOfFrame(); while (duration > 0) { // Don't count down if the game is paused, and don't loop as well. if(Instance.isPaused) { yield return w; continue; } // Set the time back to zero, since unpausing sets it back to 1. Time.timeScale = Instance.lastTimeScale * slowMultiplier; // Counts down. duration -= Time.unscaledDeltaTime; yield return w; } // Save the last time scale we want to restore to. float timeScaleToRestore = Instance.lastTimeScale; // Signal that the stop is finished. Instance.lastTimeScale = -1; stopGameCoroutine = null; // If a restore delay is set, restore the time scale gradually. if (restoreDelay > 0) { // Moves the timescale from the value it is set to, to its original value. float currentTimeScale = Instance.lastTimeScale * slowMultiplier; float restoreSpeed = (Instance.lastTimeScale - currentTimeScale) / restoreDelay; while (currentTimeScale < Instance.lastTimeScale) { // Stop this if the game is paused. if (Instance.isPaused) { yield return w; continue; } // End this coroutine if another stop has started. if (isStopped) yield break; // Set the timescale to the current value this frame. currentTimeScale += restoreSpeed * Time.unscaledDeltaTime; Time.timeScale = currentTimeScale; // Wait for a frame. yield return w; } } // Only restore the timeScale if it is not stopped again. // Can happen if another stop fires while restoring the time scale. if (!isStopped) Time.timeScale = timeScaleToRestore; } public void SaveScene() { string currentSceneName = SceneManager.GetActiveScene().name; SaveData.Instance.sceneNames.Add(currentSceneName); } public void SaveGame() { SaveData.Instance.SavePlayerData(); } public void RespawnPlayer() { SaveData.Instance.LoadBench(); if(SaveData.Instance.benchSceneName != null) //load the bench's scene if it exists. { SceneManager.LoadScene(SaveData.Instance.benchSceneName); } if(SaveData.Instance.benchPos != null) //set the respawn point to the bench's position. { respawnPoint = SaveData.Instance.benchPos; } else { respawnPoint = defaultRespawnPoint; } PlayerController.Instance.transform.position = respawnPoint; StartCoroutine(UIManager.Instance.DeactivateDeathScreen()); PlayerController.Instance.Respawned(); } }
The newly-added Stop()
is a function that takes 0 to 3 arguments. It can be called anywhere in the game with:
GameManager.Stop(); // Stops the game for 0.5 seconds, and gradually restores the original time scale over 0.1 seconds.
If you want to modify the duration of the stop, provide a float argument to it:
GameManager.Stop(1.2f); // Causes a 1.2 second stop instead.
To introduce a gradual restoration of the time scale after the stop, supply a second variable.
GameManager.Stop(1.2f, 0.2f); // Restores the timescale over 0.2 seconds after the stop ends.
Finally, if you don’t want Stop()
to stop time completely, supply a non-zero float as the 3rd argument.
GameManager.Stop(1.2f, 0.2f, 0.05f); // The stop will cause time to move at 5% of the original speed.
The 3rd argument creates a near-stop effect like this, while still maintaining some small degree of movement—perfect if you want to introduce some style to your stops.
b. Explaining the Stop()
function
The newly-added Stop()
function does 2 things:
- It checks if an existing
stopGameCoroutine
is already running, and if not; - It will call
HandleStopGame()
as a coroutine, and save it understopGameCoroutine
.
By doing this, we ensure that another stop cannot trigger when the game is already stopped.
We also add a getter called isStopped
below the stopGameCoroutine
variable that we introduce in the GameManager
. This is just a simpler way for us to check—from other scripts—whether a stop is currently in process.
static Coroutine stopGameCoroutine; public static bool isStopped { get { return stopGameCoroutine != null; } }
c. Explaining HandleStopGame()
The HandleStopGame()
coroutine then handles the rest of the work to stop the game. It does a couple of things:
- The first thing it does is save the current time scale the game is running at, provided the variable is freed up. This is explained in further detail below.
- The
timeScale
is then set to a multiple of theslowMultiplier
argument (3rd argument), to cause the time slow / stop. - We then start a loop that subtracts
Time.unscaledDeltaTime
from the duration of the stop, until the duration reduces to 0. It is important here that we work withTime.unscaledDeltaTime
, because changing the time scale will affect the value ofTime.deltaTime
. - Once the duration reaches 0, we declare that the stop is over by setting
stopGameCoroutine
tonull
. We then run another loop to gradually restore the time scale back to its original value. This loop will run until the time scale is restored, or terminate itself if another stop is triggered while the time scale restoration is still ongoing.
d. The importance of lastTimeScale
The newly-introduced lastTimeScale
variable is very important for the GameManager
script. This is because—on top of allowing us to restore the original time scale before pausing or stopping—it also syncs up both the Pause()
and Stop()
functions in the GameManager
. Without this variable, it will be possible for the player to use pausing and unpausing to cut short the time stops.
Imagine the following scenario:
- The player gets hit by an enemy, causing a 0.5-second time-stop.
- Immediately, the player pauses the game. This causes pause to set the time scale to 0 again, which doesn’t affect anything (yet).
- Upon unpausing the game, the time scale is restored back to 1, before the time-stop finishes.
As you can see, without syncing the time-stopping mechanics of both functionalities, they can very easily interfere with one another.
The lastTimeScale
variable helps both functionalities work in tandem, because:
- It serves as an indicator of whether there is any time-stop in process before we try to trigger another time-stop. Whenever a pause or stop ends,
lastTimeScale
is always set to -1 to indicate that no time-stops are in place. - If a
lastTimeScale
value is set, both functionalities will avoid writing a new value to it. Otherwise, if you pause the game during a time-stop,lastTimeScale
will be set to 0 and freeze the game forever—since resuming the game will set the time scale tolastTimeScale
(which is 0).
Notice in the edits above that the Pause()
function has also been modified. We have added another check to make sure that, when unpausing, we only restore the time scale if no stops are currently running. This is to ensure the unpausing does not interfere with the functionality of the stop.
public void Pause(bool b) {
if(b)
{
// Save the timescale we will restore to.
if(lastTimeScale < 0)
lastTimeScale = Time.timeScale;
Time.timeScale = 0f;
}
else
{
if(!isStopped)
{
Time.timeScale = lastTimeScale;
lastTimeScale = -1;
}
}
isPaused = b;
}
4. Updating the rest of the scripts
Now that we have made our new GameManager.Stop()
function, we need to update the rest of our scripts to use this new function, instead of the old HitStopTime()
from PlayerController
.
a. Updating PlayerController
For the PlayerController
script, we remove the old HitStopTime()
function, as well as the RestoreTimeScale()
function, and slightly modify the way the Flip()
function works:
PlayerController.cs
using System.Collections; using System.Collections.Generic; using UnityEditor.Build; using UnityEngine; public class PlayerController : MonoBehaviour { [Header("Horizontal Movement Settings:")] [SerializeField] private float walkSpeed = 1; //sets the players movement speed on the ground [Space(5)] [Header("Vertical Movement Settings")] [SerializeField] private float jumpForce = 45f; //sets how hight the player can jump private float jumpBufferCounter = 0; //stores the jump button input [SerializeField] private float jumpBufferFrames; //sets the max amount of frames the jump buffer input is stored private float coyoteTimeCounter = 0; //stores the Grounded() bool [SerializeField] private float coyoteTime; ////sets the max amount of frames the Grounded() bool is stored private int airJumpCounter = 0; //keeps track of how many times the player has jumped in the air [SerializeField] private int maxAirJumps; //the max no. of air jumps [SerializeField] private int maxFallingSpeed; //the max no. of air jumps private float gravity; //stores the gravity scale at start [Space(5)] [Header("Wall Jump Settings")] [SerializeField] private float wallSlidingSpeed = 2f; [SerializeField] private Transform wallCheck; [SerializeField] private LayerMask wallLayer; [SerializeField] private float wallJumpingDuration; [SerializeField] private Vector2 wallJumpingPower; float wallJumpingDirection; bool isWallSliding; bool isWallJumping; [Space(5)] [Header("Ground Check Settings:")] [SerializeField] private Transform groundCheckPoint; //point at which ground check happens [SerializeField] private float groundCheckY = 0.2f; //how far down from ground chekc point is Grounded() checked [SerializeField] private float groundCheckX = 0.5f; //how far horizontally from ground chekc point to the edge of the player is [SerializeField] private LayerMask whatIsGround; //sets the ground layer [Space(5)] [Header("Dash Settings")] [SerializeField] private float dashSpeed; //speed of the dash [SerializeField] private float dashTime; //amount of time spent dashing [SerializeField] private float dashCooldown; //amount of time between dashes [SerializeField] GameObject dashEffect; private bool canDash = true, dashed; [Space(5)] [Header("Attack Settings:")] [SerializeField] private Transform SideAttackTransform; //the middle of the side attack area [SerializeField] private Vector2 SideAttackArea; //how large the area of side attack is [SerializeField] private Transform UpAttackTransform; //the middle of the up attack area [SerializeField] private Vector2 UpAttackArea; //how large the area of side attack is [SerializeField] private Transform DownAttackTransform; //the middle of the down attack area [SerializeField] private Vector2 DownAttackArea; //how large the area of down attack is [SerializeField] private LayerMask attackableLayer; //the layer the player can attack and recoil off of [SerializeField] private float timeBetweenAttack; private float timeSinceAttack; [SerializeField] private float damage; //the damage the player does to an enemy [SerializeField] private GameObject slashEffect; //the effect of the slashs bool restoreTime; float restoreTimeSpeed; [Space(5)] [Header("Recoil Settings:")] [SerializeField] private int recoilXSteps = 5; //how many FixedUpdates() the player recoils horizontally for [SerializeField] private int recoilYSteps = 5; //how many FixedUpdates() the player recoils vertically for [SerializeField] private float recoilXSpeed = 100; //the speed of horizontal recoil [SerializeField] private float recoilYSpeed = 100; //the speed of vertical recoil private int stepsXRecoiled, stepsYRecoiled; //the no. of steps recoiled horizontally and verticall [Space(5)] [Header("Health Settings")] public int health; public int maxHealth; public int maxTotalHealth = 10; public int heartShards; [SerializeField] GameObject bloodSpurt; [SerializeField] float hitFlashSpeed; public delegate void OnHealthChangedDelegate(); [HideInInspector] public OnHealthChangedDelegate onHealthChangedCallback; float healTimer; [SerializeField] float timeToHeal; [Space(5)] [Header("Mana Settings")] [SerializeField] UnityEngine.UI.Image manaStorage; [SerializeField] float mana; [SerializeField] float manaDrainSpeed; [SerializeField] float manaGain; public bool halfMana; public ManaOrbsHandler manaOrbsHandler; public int orbShard; public int manaOrbs; [Space(5)] [Header("Spell Settings")] //spell stats [SerializeField] float manaSpellCost = 0.3f; [SerializeField] float timeBetweenCast = 0.5f; [SerializeField] float spellDamage; //upspellexplosion and downspellfireball [SerializeField] float downSpellForce; // desolate dive only //spell cast objects [SerializeField] GameObject sideSpellFireball; [SerializeField] GameObject upSpellExplosion; [SerializeField] GameObject downSpellFireball; float timeSinceCast; float castOrHealTimer; [Space(5)] [Header("Camera Stuff")] [SerializeField] private float playerFallSpeedThreshold = -10; [Space(5)] [Header("Audio")] [SerializeField] AudioClip landingSound; [SerializeField] AudioClip jumpSound; [SerializeField] AudioClip dashAndAttackSound; [SerializeField] AudioClip spellCastSound; [SerializeField] AudioClip hurtSound; [HideInInspector] public PlayerStateList pState; [HideInInspector] public Rigidbody2D rb; private Animator anim; private SpriteRenderer sr; private AudioSource audioSource; //Input Variables private float xAxis, yAxis; private bool attack = false; bool openMap; bool openInventory; private bool canFlash = true; private bool landingSoundPlayed; public static PlayerController Instance; //unlocking public bool unlockedWallJump; public bool unlockedDash; public bool unlockedVarJump; public bool unlockedSideCast; public bool unlockedUpCast; public bool unlockedDownCast; private void Awake() { if (Instance != null && Instance != this) { Destroy(gameObject); } else { Instance = this; } DontDestroyOnLoad(gameObject); } // Start is called before the first frame update void Start() { pState = GetComponent<PlayerStateList>(); rb = GetComponent<Rigidbody2D>(); sr = GetComponent<SpriteRenderer>(); anim = GetComponent<Animator>(); manaOrbsHandler = FindObjectOfType<ManaOrbsHandler>(); audioSource = GetComponent<AudioSource>(); gravity = rb.gravityScale; Mana = mana; manaStorage.fillAmount = Mana; Health = maxHealth; SaveData.Instance.LoadPlayerData(); if (manaOrbs > 3) { manaOrbs = 3; } if (halfMana) { UIManager.Instance.SwitchMana(UIManager.ManaState.HalfMana); } else { UIManager.Instance.SwitchMana(UIManager.ManaState.FullMana); } if (onHealthChangedCallback != null) onHealthChangedCallback.Invoke(); if (Health == 0) { pState.alive = false; GameManager.Instance.RespawnPlayer(); } } private void OnDrawGizmos() { Gizmos.color = Color.red; Gizmos.DrawWireCube(SideAttackTransform.position, SideAttackArea); Gizmos.DrawWireCube(UpAttackTransform.position, UpAttackArea); Gizmos.DrawWireCube(DownAttackTransform.position, DownAttackArea); } // Update is called once per frame void Update() { if (pState.cutscene || GameManager.Instance.isPaused) return; if (pState.alive) { GetInputs(); ToggleMap(); ToggleInventory(); } UpdateJumpVariables();RestoreTimeScale();UpdateCameraYDampForPlayerFall(); if (pState.dashing) return; FlashWhileInvincible(); if (!pState.alive) return; if (!isWallJumping) { Move(); } Heal(); CastSpell(); if (pState.healing) return; if (!isWallJumping) { Flip(); Jump(); } if (unlockedWallJump) { WallSlide(); WallJump(); } if (unlockedDash) { StartDash(); } Attack(); } private void OnTriggerEnter2D(Collider2D _other) //for up and down cast spell { if(_other.GetComponent<Enemy>() != null && pState.casting) { _other.GetComponent<Enemy>().EnemyGetsHit(spellDamage, (_other.transform.position - transform.position).normalized, -recoilYSpeed); } } private void FixedUpdate() { if (pState.cutscene) return; if (pState.dashing || pState.healing) return; Recoil(); } void GetInputs() { if (GameManager.Instance.isPaused || GameManager.isStopped) return; xAxis = Input.GetAxisRaw("Horizontal"); yAxis = Input.GetAxisRaw("Vertical"); attack = Input.GetButtonDown("Attack"); openMap = Input.GetButton("Map"); openInventory = Input.GetButton("Inventory"); if (Input.GetButton("Cast/Heal")) { castOrHealTimer += Time.deltaTime; } } void ToggleMap() { if(openMap) { UIManager.Instance.mapHandler.SetActive(true); } else { UIManager.Instance.mapHandler.SetActive(false); } } void ToggleInventory() { if (openInventory) { UIManager.Instance.inventory.SetActive(true); } else { UIManager.Instance.inventory.SetActive(false); } } void Flip() { if (xAxis < 0) {transform.eulerAngles = new Vector2(0, 180);transform.localScale = new Vector2(-Mathf.Abs(transform.localScale.x), transform.localScale.y); pState.lookingRight = false; } else if (xAxis > 0) {transform.eulerAngles = new Vector2(0, 0);transform.localScale = new Vector2(Mathf.Abs(transform.localScale.x), transform.localScale.y); pState.lookingRight = true; } } private void Move() { if (pState.healing) rb.velocity = new Vector2(0, 0); rb.velocity = new Vector2(walkSpeed * xAxis, rb.velocity.y); anim.SetBool("Walking", rb.velocity.x != 0 && Grounded()); } void UpdateCameraYDampForPlayerFall() { //if falling past a certain speed threshold if (rb.velocity.y < playerFallSpeedThreshold && !CameraManager.Instance.isLerpingYDamping && !CameraManager.Instance.hasLerpedYDamping) { StartCoroutine(CameraManager.Instance.LerpYDamping(true)); } //if standing stil or moving up if(rb.velocity.y >= 0 && !CameraManager.Instance.isLerpingYDamping && CameraManager.Instance.hasLerpedYDamping) { //reset camera function CameraManager.Instance.hasLerpedYDamping = false; StartCoroutine(CameraManager.Instance.LerpYDamping(false)); } } void StartDash() { if (Input.GetButtonDown("Dash") && canDash && !dashed) { StartCoroutine(Dash()); dashed = true; } if (Grounded()) { dashed = false; } } IEnumerator Dash() { canDash = false; pState.dashing = true; anim.SetTrigger("Dashing"); audioSource.PlayOneShot(dashAndAttackSound); rb.gravityScale = 0; int _dir = pState.lookingRight ? 1 : -1; rb.velocity = new Vector2(_dir * dashSpeed, 0); if (Grounded()) Instantiate(dashEffect, transform); yield return new WaitForSeconds(dashTime); rb.gravityScale = gravity; pState.dashing = false; yield return new WaitForSeconds(dashCooldown); canDash = true; } public IEnumerator WalkIntoNewScene(Vector2 _exitDir, float _delay) { pState.invincible = true; //If exit direction is upwards if (_exitDir.y != 0) { rb.velocity = jumpForce * _exitDir; } //If exit direction requires horizontal movement if (_exitDir.x != 0) { xAxis = _exitDir.x > 0 ? 1 : -1; Move(); } Flip(); yield return new WaitForSeconds(_delay); print("cutscene played"); pState.invincible = false; pState.cutscene = false; } void Attack() { timeSinceAttack += Time.deltaTime; if (attack && timeSinceAttack >= timeBetweenAttack) { timeSinceAttack = 0; anim.SetTrigger("Attacking"); audioSource.PlayOneShot(dashAndAttackSound); if (yAxis == 0 || yAxis < 0 && Grounded()) { int _recoilLeftOrRight = pState.lookingRight ? 1 : -1; Hit(SideAttackTransform, SideAttackArea, ref pState.recoilingX, Vector2.right * _recoilLeftOrRight, recoilXSpeed); Instantiate(slashEffect, SideAttackTransform); } else if (yAxis > 0) { Hit(UpAttackTransform, UpAttackArea, ref pState.recoilingY, Vector2.up, recoilYSpeed); SlashEffectAtAngle(slashEffect, 80, UpAttackTransform); } else if (yAxis < 0 && !Grounded()) { Hit(DownAttackTransform, DownAttackArea, ref pState.recoilingY, Vector2.down, recoilYSpeed); SlashEffectAtAngle(slashEffect, -90, DownAttackTransform); } } } void Hit(Transform _attackTransform, Vector2 _attackArea, ref bool _recoilBool, Vector2 _recoilDir, float _recoilStrength) { Collider2D[] objectsToHit = Physics2D.OverlapBoxAll(_attackTransform.position, _attackArea, 0, attackableLayer); List<Enemy> hitEnemies = new List<Enemy>(); if (objectsToHit.Length > 0) { _recoilBool = true; } for (int i = 0; i < objectsToHit.Length; i++) { Enemy e = objectsToHit[i].GetComponent<Enemy>(); if (e && !hitEnemies.Contains(e)) { e.EnemyGetsHit(damage, _recoilDir, _recoilStrength); hitEnemies.Add(e); if (objectsToHit[i].CompareTag("Enemy")) { if (!halfMana && Mana < 1 || (halfMana && Mana < 0.5)) { Mana += manaGain; } else { manaOrbsHandler.UpdateMana(manaGain * 3); } } } } } void SlashEffectAtAngle(GameObject _slashEffect, int _effectAngle, Transform _attackTransform) { _slashEffect = Instantiate(_slashEffect, _attackTransform); _slashEffect.transform.eulerAngles = new Vector3(0, 0, _effectAngle); _slashEffect.transform.localScale = new Vector2(transform.localScale.x, transform.localScale.y); } void Recoil() { if (pState.recoilingX) { if (pState.lookingRight) { rb.velocity = new Vector2(-recoilXSpeed, 0); } else { rb.velocity = new Vector2(recoilXSpeed, 0); } } if (pState.recoilingY) { rb.gravityScale = 0; if (yAxis < 0) { rb.velocity = new Vector2(rb.velocity.x, recoilYSpeed); } else { rb.velocity = new Vector2(rb.velocity.x, -recoilYSpeed); } airJumpCounter = 0; } else { rb.gravityScale = gravity; } //stop recoil if (pState.recoilingX && stepsXRecoiled < recoilXSteps) { stepsXRecoiled++; } else { StopRecoilX(); } if (pState.recoilingY && stepsYRecoiled < recoilYSteps) { stepsYRecoiled++; } else { StopRecoilY(); } if (Grounded()) { StopRecoilY(); } } void StopRecoilX() { stepsXRecoiled = 0; pState.recoilingX = false; } void StopRecoilY() { stepsYRecoiled = 0; pState.recoilingY = false; } public void TakeDamage(float _damage) { if(pState.alive) { audioSource.PlayOneShot(hurtSound); Health -= Mathf.RoundToInt(_damage); if(Health <= 0) { Health = 0; StartCoroutine(Death()); } else { StartCoroutine(StopTakingDamage()); } } } IEnumerator StopTakingDamage() { pState.invincible = true; GameObject _bloodSpurtParticles = Instantiate(bloodSpurt, transform.position, Quaternion.identity); Destroy(_bloodSpurtParticles, 1.5f); anim.SetTrigger("TakeDamage"); yield return new WaitForSeconds(1f); pState.invincible = false; } IEnumerator Flash() { sr.enabled = !sr.enabled; canFlash = false; yield return new WaitForSeconds(0.1f); canFlash = true; } void FlashWhileInvincible() { if (pState.invincible && !pState.cutscene) { if(Time.timeScale > 0.2 && canFlash) { StartCoroutine(Flash()); } } else { sr.enabled = true; } }void RestoreTimeScale() { if (restoreTime) { if (Time.timeScale < 1) { Time.timeScale += Time.unscaledDeltaTime * restoreTimeSpeed; } else { Time.timeScale = 1; restoreTime = false; } } } public void HitStopTime(float _newTimeScale, int _restoreSpeed, float _delay) { restoreTimeSpeed = _restoreSpeed; if (_delay > 0) { StopCoroutine(StartTimeAgain(_delay)); StartCoroutine(StartTimeAgain(_delay)); } else { restoreTime = true; } Time.timeScale = _newTimeScale; }IEnumerator StartTimeAgain(float _delay) { yield return new WaitForSecondsRealtime(_delay); restoreTime = true; } IEnumerator Death() { pState.alive = false; Time.timeScale = 1f; GameObject _bloodSpurtParticles = Instantiate(bloodSpurt, transform.position, Quaternion.identity); Destroy(_bloodSpurtParticles, 1.5f); anim.SetTrigger("Death"); rb.constraints = RigidbodyConstraints2D.FreezePosition; GetComponent<BoxCollider2D>().enabled = false; yield return new WaitForSeconds(0.9f); StartCoroutine(UIManager.Instance.ActivateDeathScreen()); yield return new WaitForSeconds(0.1f); Instantiate(GameManager.Instance.shade, transform.position, Quaternion.identity); SaveData.Instance.SavePlayerData(); } public void Respawned() { if (!pState.alive) { rb.constraints = RigidbodyConstraints2D.None; rb.constraints = RigidbodyConstraints2D.FreezeRotation; GetComponent<BoxCollider2D>().enabled = true; pState.alive = true; halfMana = true; UIManager.Instance.SwitchMana(UIManager.ManaState.HalfMana); Mana = 0; Health = maxHealth; anim.Play("Player_Idle"); } } public void RestoreMana() { halfMana = false; UIManager.Instance.SwitchMana(UIManager.ManaState.FullMana); } public int Health { get { return health; } set { if (health != value) { health = Mathf.Clamp(value, 0, maxHealth); if (onHealthChangedCallback != null) { onHealthChangedCallback.Invoke(); } } } } void Heal() { if (Input.GetButton("Cast/Heal") && castOrHealTimer > 0.1f && Health < maxHealth && Mana > 0 && Grounded() && !pState.dashing) { pState.healing = true; anim.SetBool("Healing", true); //healing healTimer += Time.deltaTime; if (healTimer >= timeToHeal) { Health++; healTimer = 0; } //drain mana manaOrbsHandler.usedMana = true; manaOrbsHandler.countDown = 3f; Mana -= Time.deltaTime * manaDrainSpeed; } else { pState.healing = false; anim.SetBool("Healing", false); healTimer = 0; } } public float Mana { get { return mana; } set { //if mana stats change if (mana != value) { if(!halfMana) { mana = Mathf.Clamp(value, 0, 1); } else { mana = Mathf.Clamp(value, 0, 0.5f); } manaStorage.fillAmount = Mana; } } } void CastSpell() { if (Input.GetButtonUp("Cast/Heal") && castOrHealTimer <= 0.1f && timeSinceCast >= timeBetweenCast && Mana >= manaSpellCost) { pState.casting = true; timeSinceCast = 0; StartCoroutine(CastCoroutine()); } else { timeSinceCast += Time.deltaTime; } if (!Input.GetButton("Cast/Heal")) { castOrHealTimer = 0; } if (Grounded()) { //disable downspell if on the ground downSpellFireball.SetActive(false); } //if down spell is active, force player down until grounded if(downSpellFireball.activeInHierarchy) { rb.velocity += downSpellForce * Vector2.down; } } IEnumerator CastCoroutine() { //side cast if ((yAxis == 0 || (yAxis < 0 && Grounded())) && unlockedSideCast) { audioSource.PlayOneShot(spellCastSound); anim.SetBool("Casting", true); yield return new WaitForSeconds(0.15f); GameObject _fireBall = Instantiate(sideSpellFireball, SideAttackTransform.position, Quaternion.identity); //flip fireball if(pState.lookingRight) { _fireBall.transform.eulerAngles = Vector3.zero; // if facing right, fireball continues as per normal } else { _fireBall.transform.eulerAngles = new Vector2(_fireBall.transform.eulerAngles.x, 180); //if not facing right, rotate the fireball 180 deg } pState.recoilingX = true; Mana -= manaSpellCost; manaOrbsHandler.usedMana = true; manaOrbsHandler.countDown = 3f; yield return new WaitForSeconds(0.35f); } //up cast else if( yAxis > 0 && unlockedUpCast) { audioSource.PlayOneShot(spellCastSound); anim.SetBool("Casting", true); yield return new WaitForSeconds(0.15f); Instantiate(upSpellExplosion, transform); rb.velocity = Vector2.zero; Mana -= manaSpellCost; manaOrbsHandler.usedMana = true; manaOrbsHandler.countDown = 3f; yield return new WaitForSeconds(0.35f); } //down cast else if((yAxis < 0 && !Grounded()) && unlockedDownCast) { audioSource.PlayOneShot(spellCastSound); anim.SetBool("Casting", true); yield return new WaitForSeconds(0.15f); downSpellFireball.SetActive(true); Mana -= manaSpellCost; manaOrbsHandler.usedMana = true; manaOrbsHandler.countDown = 3f; yield return new WaitForSeconds(0.35f); } anim.SetBool("Casting", false); pState.casting = false; } public bool Grounded() { if (Physics2D.Raycast(groundCheckPoint.position, Vector2.down, groundCheckY, whatIsGround) || Physics2D.Raycast(groundCheckPoint.position + new Vector3(groundCheckX, 0, 0), Vector2.down, groundCheckY, whatIsGround) || Physics2D.Raycast(groundCheckPoint.position + new Vector3(-groundCheckX, 0, 0), Vector2.down, groundCheckY, whatIsGround)) { return true; } else { return false; } } void Jump() { if (jumpBufferCounter > 0 && coyoteTimeCounter > 0 && !pState.jumping) { if (Input.GetButtonDown("Jump")) { audioSource.PlayOneShot(jumpSound); } rb.velocity = new Vector3(rb.velocity.x, jumpForce); pState.jumping = true; } if (!Grounded() && airJumpCounter < maxAirJumps && Input.GetButtonDown("Jump") && unlockedVarJump) { audioSource.PlayOneShot(jumpSound); pState.jumping = true; airJumpCounter++; rb.velocity = new Vector3(rb.velocity.x, jumpForce); } if (Input.GetButtonUp("Jump") && rb.velocity.y > 3) { pState.jumping = false; rb.velocity = new Vector2(rb.velocity.x, 0); } rb.velocity = new Vector2(rb.velocity.x, Mathf.Clamp(rb.velocity.y, -maxFallingSpeed, rb.velocity.y)); anim.SetBool("Jumping", !Grounded()); } void UpdateJumpVariables() { if (Grounded()) { if (!landingSoundPlayed) { audioSource.PlayOneShot(landingSound); landingSoundPlayed = true; } pState.jumping = false; coyoteTimeCounter = coyoteTime; airJumpCounter = 0; } else { coyoteTimeCounter -= Time.deltaTime; landingSoundPlayed = false; } if (Input.GetButtonDown("Jump")) { jumpBufferCounter = jumpBufferFrames; } else { jumpBufferCounter = jumpBufferCounter - Time.deltaTime * 10; } } private bool Walled() { return Physics2D.OverlapCircle(wallCheck.position, 0.2f, wallLayer); } void WallSlide() { if(Walled() && !Grounded() && xAxis != 0) { isWallSliding = true; rb.velocity = new Vector2(rb.velocity.x, Mathf.Clamp(rb.velocity.y, -wallSlidingSpeed, float.MaxValue)); } else { isWallSliding = false; } } void WallJump() { if (isWallSliding) { isWallJumping = false; wallJumpingDirection = !pState.lookingRight ? 1 : -1; CancelInvoke(nameof(StopWallJumping)); } if (Input.GetButtonDown("Jump") && isWallSliding) { audioSource.PlayOneShot(jumpSound); isWallJumping = true; rb.velocity = new Vector2(wallJumpingDirection * wallJumpingPower.x, wallJumpingPower.y); dashed = false; airJumpCounter = 0; pState.lookingRight = !pState.lookingRight; float jumpDirection = pState.lookingRight ? 0 : 180; transform.eulerAngles = new Vector2(transform.eulerAngles.x, jumpDirection); Invoke(nameof(StopWallJumping), wallJumpingDuration); } } void StopWallJumping() { isWallJumping = false; transform.eulerAngles = new Vector2(transform.eulerAngles.x, 0); } }
The modification to the Flip()
function rectifies a problem with stuttering movement on the player at low time scales. For some reason, Unity has trouble processing movement for 2D objects when you rotate them—so we flip the scaling of the character instead of rotating it.
Another thing to take note of is the addition of the following line in GetInputs()
.
if (GameManager.Instance.isPaused || GameManager.isStopped) return;
This prevents the player character from being able to flip themselves when the game is paused or stopped like below:
Another minor change is the addition of a check to see if onHealthChangedCallback
is null before we call onHealthChangedCallback.Invoke();
from Start()
. This is because when you don’t have an existing save file, this line can cause a NullReferenceException
because nothing is assigned to that delegate.
After the save system is set up, we are also going to improve the PlayerController
script and make it more organised and scalable. This will make it easier for us to cover how to add more advanced features in Hollow Knight, like charms. Keep your eyes posted!
b. Replacing HitStopTime()
in other scripts
Besides the PlayerController
script, we will also need to replace HitStopTime()
with GameManager.Stop()
in other scripts. Once you remove the HitStopTime()
function from PlayerController
, you should get a list of errors on the Unity Editor Console that will tell you which scripts need to be updated, as you will now be missing a HitStopTime()
function.
In our case, these are the scripts we needed to update:
Enemy.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Enemy : MonoBehaviour { [SerializeField] protected float health; [SerializeField] protected float recoilLength; [SerializeField] protected float recoilFactor; [SerializeField] protected bool isRecoiling = false; [SerializeField] public float speed; [SerializeField] public float damage; [SerializeField] protected GameObject orangeBlood; [SerializeField] AudioClip hurtSound; protected float recoilTimer; [HideInInspector] public Rigidbody2D rb; protected SpriteRenderer sr; public Animator anim; protected AudioSource audioSource; protected enum EnemyStates { //Crawler Crawler_Idle, Crawler_Flip, //Bat Bat_Idle, Bat_Chase, Bat_Stunned, Bat_Death, //Charger Charger_Idle, Charger_Suprised, Charger_Charge, //Shade Shade_Idle, Shade_Chase, Shade_Stunned, Shade_Death, //THK THK_Stage1, THK_Stage2, THK_Stage3, THK_Stage4 } protected EnemyStates currentEnemyState; protected virtual EnemyStates GetCurrentEnemyState { get { return currentEnemyState; } set { if(currentEnemyState != value) { currentEnemyState = value; ChangeCurrentAnimation(); } } } // Start is called before the first frame update protected virtual void Start() { rb = GetComponent<Rigidbody2D>(); sr = GetComponent<SpriteRenderer>(); anim = GetComponent<Animator>(); audioSource = GetComponent<AudioSource>(); } // Update is called once per frame protected virtual void Update() { if (GameManager.Instance.isPaused) return; if(isRecoiling) { if(recoilTimer < recoilLength) { recoilTimer += Time.deltaTime; } else { isRecoiling = false; recoilTimer = 0; } } else { UpdateEnemyStates(); } } public virtual void EnemyGetsHit(float _damageDone, Vector2 _hitDirection, float _hitForce) { health -= _damageDone; if(!isRecoiling) { audioSource.PlayOneShot(hurtSound); GameObject _orangeBlood = Instantiate(orangeBlood, transform.position, Quaternion.identity); Destroy(_orangeBlood, 5.5f); rb.velocity = _hitForce * recoilFactor * _hitDirection; isRecoiling = true; } } protected virtual void OnCollisionStay2D(Collision2D _other) { if(_other.gameObject.CompareTag("Player") && !PlayerController.Instance.pState.invincible && health > 0) { Attack(); if(PlayerController.Instance.pState.alive) {PlayerController.Instance.HitStopTime(0, 5, 0.5f);GameManager.Stop(); } } } protected virtual void Death(float _destroyTime) { Destroy(gameObject, _destroyTime); } protected virtual void UpdateEnemyStates() { } protected virtual void ChangeCurrentAnimation() { } protected void ChangeState(EnemyStates _newState) { GetCurrentEnemyState = _newState; } protected virtual void Attack() { PlayerController.Instance.TakeDamage(damage); } }
Boss_Lunge.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Boss_Lunge : StateMachineBehaviour { Rigidbody2D rb; // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { rb = animator.GetComponentInParent<Rigidbody2D>(); } // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { rb.gravityScale = 0; int _dir = TheHollowKnight.Instance.facingRight ? 1 : -1; rb.velocity = new Vector2(_dir * (TheHollowKnight.Instance.speed * 5), 0f); if(Vector2.Distance(PlayerController.Instance.transform.position, rb.position) <= TheHollowKnight.Instance.attackRange && !TheHollowKnight.Instance.damagedPlayer && !PlayerController.Instance.pState.invincible) { PlayerController.Instance.TakeDamage(TheHollowKnight.Instance.damage); TheHollowKnight.Instance.damagedPlayer = true; if (PlayerController.Instance.pState.alive) {PlayerController.Instance.HitStopTime(0, 5, 0.5f);GameManager.Stop(); } } } // OnStateExit is called when a transition ends and the state machine finishes evaluating this state override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { } }
THKEvents.cs
using System.Collections; using UnityEngine; public class THKEvents : MonoBehaviour { void SlashDamagePlayer() { if (PlayerController.Instance.transform.position.x - transform.position.x != 0) { Hit(TheHollowKnight.Instance.SideAttackTransform, TheHollowKnight.Instance.SideAttackArea); } else if (PlayerController.Instance.transform.position.y > transform.position.y) { Hit(TheHollowKnight.Instance.UpAttackTransform, TheHollowKnight.Instance.UpAttackArea); } else if (PlayerController.Instance.transform.position.y < transform.position.y) { Hit(TheHollowKnight.Instance.DownAttackTransform, TheHollowKnight.Instance.DownAttackArea); } } void Hit(Transform _attackTransform, Vector2 _attackArea) { Collider2D[] _objectsToHit = Physics2D.OverlapBoxAll(_attackTransform.position, _attackArea, 0); for (int i = 0; i < _objectsToHit.Length; i++) { if (_objectsToHit[i].GetComponent<PlayerController>() != null && !PlayerController.Instance.pState.invincible) { _objectsToHit[i].GetComponent<PlayerController>().TakeDamage(TheHollowKnight.Instance.damage); if (PlayerController.Instance.pState.alive) {PlayerController.Instance.HitStopTime(0, 5, 0.5f);GameManager.Stop(); } } } } void Parrying() { TheHollowKnight.Instance.parrying = true; } void BendDownCheck() { if(TheHollowKnight.Instance.barrageAttack) { StartCoroutine(BarrageAttackTransition()); } if(TheHollowKnight.Instance.outbreakAttack) { StartCoroutine(OutbreakAttackTransition()); } if(TheHollowKnight.Instance.bounceAttack) { TheHollowKnight.Instance.anim.SetTrigger("Bounce1"); } } void BarrageOrOutbreak() { if(TheHollowKnight.Instance.barrageAttack) { TheHollowKnight.Instance.StartCoroutine(TheHollowKnight.Instance.Barrage()); } if (TheHollowKnight.Instance.outbreakAttack) { TheHollowKnight.Instance.StartCoroutine(TheHollowKnight.Instance.Outbreak()); } } IEnumerator BarrageAttackTransition() { yield return new WaitForSeconds(1f); TheHollowKnight.Instance.anim.SetBool("Cast", true); } IEnumerator OutbreakAttackTransition() { yield return new WaitForSeconds(1f); TheHollowKnight.Instance.anim.SetBool("Cast", true); } void DestroyAfterDeath() { SpawnBoss.Instance.IsNotTrigger(); TheHollowKnight.Instance.DestroyAfterDeath(); GameManager.Instance.THKDefeated = true; SaveData.Instance.SaveBossData(); SaveData.Instance.SavePlayerData(); } }
BarrageFireball.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; public class BarrageFireball : MonoBehaviour { [SerializeField] Vector2 startForceMinMax; [SerializeField] float turnSpeed = 0.5f; Rigidbody2D rb; // Start is called before the first frame update void Start() { rb = GetComponent<Rigidbody2D>(); Destroy(gameObject, 4f); rb.AddForce(transform.right * Random.Range(startForceMinMax.x, startForceMinMax.y), ForceMode2D.Impulse); } // Update is called once per frame void Update() { var _dir = rb.velocity; if(_dir != Vector2.zero) { Vector3 _frontVector = Vector3.right; Quaternion _targetRotation = Quaternion.FromToRotation(_frontVector, _dir - (Vector2)transform.position); if(_dir.x > 0) { transform.rotation = Quaternion.Lerp(transform.rotation, _targetRotation, turnSpeed); transform.eulerAngles = new Vector3(transform.eulerAngles.x, 180, transform.eulerAngles.z); } else { transform.rotation = Quaternion.Lerp(transform.rotation, _targetRotation, turnSpeed); } } } private void OnTriggerEnter2D(Collider2D _other) { if (_other.tag == "Player" && !PlayerController.Instance.pState.invincible) { _other.GetComponent<PlayerController>().TakeDamage(TheHollowKnight.Instance.damage); Destroy(gameObject); if (PlayerController.Instance.pState.alive) {PlayerController.Instance.HitStopTime(0, 5, 0.5f);GameManager.Stop(); } } } }
DivingPillar.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; public class DivingPillar : MonoBehaviour { private void OnTriggerEnter2D(Collider2D _other) { if(_other.CompareTag("Player") && !PlayerController.Instance.pState.invincible) { _other.GetComponent<PlayerController>().TakeDamage(TheHollowKnight.Instance.damage); if (PlayerController.Instance.pState.alive) {PlayerController.Instance.HitStopTime(0, 5, 0.5f);GameManager.Stop(); } } } }
5. Conclusion
With time-stop properly implemented in our project, we should now be able to implement more advanced UI features in a stable and scalable manner.
In the next part, we will be exploring some improvements to the current Pickup system that we have. This will be followed by implementing a more stable, bug-free and more powerful save system.
Silver Patreons can download the project files here.