Forum begins after the advertisement:
[Part 6] Double damage on spikes
Home › Forums › Video Game Tutorial Series › Creating a Metroidvania in Unity › [Part 6] Double damage on spikes
- This topic has 11 replies, 3 voices, and was last updated 2 weeks, 3 days ago by
Nirvik Rajbhandari.
-
AuthorPosts
-
April 15, 2026 at 9:17 am #19411::
Whenever the player lands right between two spikes the player takes double damage.
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Spikes : MonoBehaviour { void Start() { } void Update() { } void OnTriggerEnter2D(Collider2D _other) { if(_other.CompareTag("Player")) { StartCoroutine(RespawnPoint()); } } IEnumerator RespawnPoint() { PlayerController.Instance.pState.invincible = true; PlayerController.Instance.pState.cutscene = true; Time.timeScale = 0; StartCoroutine(UIManager.Instance.sceneFader.Fade(SceneFader.FadeDirection.In)); PlayerController.Instance.TakeDamage(1); yield return new WaitForSecondsRealtime(1); PlayerController.Instance.transform.position = GameManager.Instance.platformingRespawnPoint; StartCoroutine(UIManager.Instance.sceneFader.Fade(SceneFader.FadeDirection.Out)); yield return new WaitForSecondsRealtime(UIManager.Instance.sceneFader.fadeTime); PlayerController.Instance.pState.invincible = false; PlayerController.Instance.pState.cutscene = false; Time.timeScale = 1; } }April 15, 2026 at 9:09 pm #19414::If you have multiple colliders on the player or on the spikes, they can trigger
OnTriggerEnter2D()more than once and cause you to take damage multiple times. To prevent this, modifyTakeDamage()on the player like so:public void TakeDamage(float _damage) { if(pState.invincible) return; Health -= Mathf.RoundToInt(_damage); StartCoroutine(StopTakingDamage()); }This is because
TakeDamage()callsStopTakingDamage(), which setspState.invincibleto true to prevent you from taking further damage.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; }April 16, 2026 at 7:25 am #19417April 16, 2026 at 5:08 pm #19418::now the player doesnt take any damage from spikes
Check whether your
StopTakingDamage()function is working properly:IEnumerator StopTakingDamage() { pState.invincible = true; Debug.Log("StopTakingDamage() start"); GameObject _bloodSpurtParticles = Instantiate(bloodSpurt, transform.position, Quaternion.identity); Destroy(_bloodSpurtParticles, 1.5f); anim.SetTrigger("TakeDamage"); yield return new WaitForSeconds(1f); pState.invincible = false; Debug.Log("StopTakingDamage() end"); }Add the lines above and show me what your Console says.
April 16, 2026 at 8:23 pm #19431::the stoptakingdamage function is working when damaged by an enemy but doesnt run at all if it takes damage from spikes
April 17, 2026 at 3:02 pm #19432April 17, 2026 at 7:48 pm #19433::here is the spikes script
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Spikes : MonoBehaviour { void Start() { } void Update() { } void OnTriggerEnter2D(Collider2D _other) { if(_other.CompareTag("Player")) { StartCoroutine(RespawnPoint()); } } IEnumerator RespawnPoint() { PlayerController.Instance.pState.invincible = true; PlayerController.Instance.pState.cutscene = true; Time.timeScale = 0; StartCoroutine(UIManager.Instance.sceneFader.Fade(SceneFader.FadeDirection.In)); PlayerController.Instance.TakeDamage(1); yield return new WaitForSecondsRealtime(1); PlayerController.Instance.transform.position = GameManager.Instance.platformingRespawnPoint; StartCoroutine(UIManager.Instance.sceneFader.Fade(SceneFader.FadeDirection.Out)); yield return new WaitForSecondsRealtime(UIManager.Instance.sceneFader.fadeTime); PlayerController.Instance.pState.invincible = false; PlayerController.Instance.pState.cutscene = false; Time.timeScale = 1; } }April 17, 2026 at 7:51 pm #19434::Also, I hope I dont sound impatient but can I get help on the charger script as well on an earlier post
April 19, 2026 at 12:57 am #19435::using System.Collections; using System.Collections.Generic; using UnityEngine; public class Spikes : MonoBehaviour { bool activatedThisFrame = false; void Start() { } void Update() { activatedThisFrame = false; } void OnTriggerEnter2D(Collider2D _other) { if(activatedThisFrame) return; if(_other.CompareTag("Player")) { StartCoroutine(RespawnPoint()); activatedThisFrame = true; } } IEnumerator RespawnPoint() { PlayerController.Instance.pState.invincible = true; PlayerController.Instance.pState.cutscene = true; Time.timeScale = 0; StartCoroutine(UIManager.Instance.sceneFader.Fade(SceneFader.FadeDirection.In)); PlayerController.Instance.TakeDamage(1); yield return new WaitForSecondsRealtime(1); PlayerController.Instance.transform.position = GameManager.Instance.platformingRespawnPoint; StartCoroutine(UIManager.Instance.sceneFader.Fade(SceneFader.FadeDirection.Out)); yield return new WaitForSecondsRealtime(UIManager.Instance.sceneFader.fadeTime); PlayerController.Instance.pState.invincible = false; PlayerController.Instance.pState.cutscene = false; Time.timeScale = 1; } }Add the portions above to prevent this from activating more than once per frame.
April 19, 2026 at 12:59 am #19436::Also, I hope I dont sound impatient but can I get help on the charger script as well on an earlier post
Share the link to the post please.
April 20, 2026 at 10:51 am #19439::Hello Nirvik, on the problem with your player not taking spike damage, I notice that your player invincible sate is set true once he collide with spikes before taking damage, if your player damage function returns when invincible state is true that may be why it doesn’t take damage from spike.
IEnumerator RespawnPoint() { PlayerController.Instance.pState.invincible = true; PlayerController.Instance.pState.cutscene = true; Time.timeScale = 0; StartCoroutine(UIManager.Instance.sceneFader.Fade(SceneFader.FadeDirection.In)); PlayerController.Instance.TakeDamage(1); yield return new WaitForSecondsRealtime(1); PlayerController.Instance.transform.position = GameManager.Instance.platformingRespawnPoint; StartCoroutine(UIManager.Instance.sceneFader.Fade(SceneFader.FadeDirection.Out)); yield return new WaitForSecondsRealtime(UIManager.Instance.sceneFader.fadeTime); PlayerController.Instance.pState.invincible = false; PlayerController.Instance.pState.cutscene = false; Time.timeScale = 1; }As your player will set invincible to true after taking, by removing the higlighted code it should allow them to take damage from spike.
- 1 anonymous person
April 23, 2026 at 10:30 am #19440 -
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: