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

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #19411
    Nirvik Rajbhandari
    Level 6
    Participant
    Helpful?
    Up
    0
    ::

    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;
        }
    }
    #19414
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    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, modify TakeDamage() on the player like so:

    public void TakeDamage(float _damage)
    {
        if(pState.invincible) return;
        Health -= Mathf.RoundToInt(_damage);
        StartCoroutine(StopTakingDamage());
    }

    This is because TakeDamage() calls StopTakingDamage(), which sets pState.invincible to 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;
    }
    #19417
    Nirvik Rajbhandari
    Level 6
    Participant
    Helpful?
    Up
    0
    ::

    now the player doesnt take any damage from spikes

    #19418
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::
    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.

    #19431
    Nirvik Rajbhandari
    Level 6
    Participant
    Helpful?
    Up
    0
    ::

    the stoptakingdamage function is working when damaged by an enemy but doesnt run at all if it takes damage from spikes

    #19432
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    Can you share your spikes code?

    #19433
    Nirvik Rajbhandari
    Level 6
    Participant
    Helpful?
    Up
    0
    ::

    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;
        }
    }
    #19434
    Nirvik Rajbhandari
    Level 6
    Participant
    Helpful?
    Up
    0
    ::

    Also, I hope I dont sound impatient but can I get help on the charger script as well on an earlier post

    #19435
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::
    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.

    #19436
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::
    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.

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: