-1

Hello Everyone I've got a Little problem. I'm trying to make the platformer damage the player as long as the player stand on the platformer every X seconds, now its working but as long as I keep on moving, if the player only stand there nothing is happened.

enter image description here

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlatformerManager : MonoBehaviour
{

    bool canTakeDmg = true;
    


    private void OnCollisionStay2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Player")
        {
            var healthComponent = collision.gameObject.GetComponent<PlayerManager>();

            if (healthComponent != null && canTakeDmg)
            {
                healthComponent.GetDamage(1);
                StartCoroutine(WaitForSeconds());
            }
        }



        IEnumerator WaitForSeconds()
        {
            canTakeDmg = false;
            yield return new WaitForSecondsRealtime(3);
            canTakeDmg = true;
        }
    }
}

the player movement - for cases I did something wrong with the player movement.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PlayerManager : MonoBehaviour
{
    #region Public Fields
    public float movmentSpeed;
    public float jumpForce;
    public Transform groundCheck;
    public LayerMask groundLayer;
    public bool isGrounded;
    public int currentHealth;
    public int maxHealth;
    public Image[] hearts;
    public Sprite fullHeart;
    public Sprite emptyHeart;

    #endregion


    #region Private Fields
    private SpriteRenderer spriteRender;
    private bool isCuteScene;
    private bool isFacingRight;
    private Rigidbody2D rb2D;
    #endregion


    Umbrella playerUmbrella;
    CameraController cameraController;

    private void Start()
    {


        rb2D = GetComponent<Rigidbody2D>();
        spriteRender = GetComponent<SpriteRenderer>();
        
    }

    void Update()
    {

        PlayerMovementHandle();
        PlayerHp();

    }


    public void PlayerMovementHandle()
    {
        GroundCheck();
        float horizontal = Input.GetAxis("Horizontal");
        Vector3 movement = new Vector3(horizontal, 0f, 0f);
        transform.position += movement * Time.deltaTime * movmentSpeed;

        //var movement = Input.GetAxis("Horizontal");
        //rb2D.velocity = new Vector2(movement, rb2D.velocity.y) * movmentSpeed * Time.deltaTime;

        Flip(horizontal);
        Jump();
     
    } 

    void GroundCheck()
    {
        isGrounded = false;

        Collider2D[] colliders = Physics2D.OverlapCircleAll(groundCheck.position, 0.2f, groundLayer);
        if (colliders.Length>0)
        {
            isGrounded = true;
        }
    }

    public void Jump()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            if (isGrounded)
            {
                //rb2D.velocity = Vector2.up * jumpForce;
                rb2D.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse);

            }
        }

    }

    public void Flip(float horiznotal)
    {
        if (horiznotal>0 &&!isFacingRight || horiznotal < 0 && isFacingRight)
        {
            isFacingRight = !isFacingRight;

            Vector3 scale = transform.localScale;

            scale.x *= -1;
            transform.localScale = scale;
        }
    }

    public void PlayerHp () {

        
        for (int i = 0; i < hearts.Length; i++)
        {

            if (i < currentHealth)
            {
                hearts[i].sprite = fullHeart;
            }
            else
            {
                hearts[i].sprite = emptyHeart;
            }


            if (i < maxHealth)
            {
                hearts[i].enabled = true;
            }
            else
            {
                hearts[i].enabled = false;
            }
        }



        if (currentHealth > maxHealth)
        {
            currentHealth = maxHealth;
        }


    }

    public void GetDamage(int amount)
    {
        currentHealth -= amount;

        if (currentHealth <=0)
        {
            //Dead animation 
            //GameOverScreen
        }
    }
}
Chen Robin
  • 25
  • 4
  • Why is your Coroutine (`WaitForSeconds`) nested inside `OnCollisionStay2D` ? Is it possible that if you stop moving the collision stops? How exactly are you moving the object? – derHugo Apr 16 '21 at 15:34
  • Because I want the player to take dmg every X seconds and for doing it I need coroutine. and as long as my player inside the collision of the plaformer I want the dmg to apply. (Ive got another scripts for the Player Manager movement. very basics I can uploaded it also) – Chen Robin Apr 16 '21 at 15:41
  • Yeah but why is the routine not simply a normal method on class level but a nested function inside of another method? And yes please add the movement code .. I think you are doing something that breaks the physics and collision detection in there – derHugo Apr 16 '21 at 15:49
  • I just uploaded it. And because I want the routine to be happing on the collision event. no? I dont know how to do it other wise – Chen Robin Apr 16 '21 at 16:43
  • Are you sure doesnt collide and leave. Not just stay? – BugFinder Apr 16 '21 at 19:23
  • As I suspected you might be breaking the physics ... whenever there is a Rigidbody involved you do not want to move it via the transform component! You should probably rather do `rb2D.velocity = new Vector2 (horizontal * movementSpeed, rb2D.velocity.y);` .. still as BugFinder days I also suspect your collision might be entering and exiting instead of being a continous collision – derHugo Apr 16 '21 at 19:51
  • Note that also changing the local scale might be an issue for the physics – derHugo Apr 16 '21 at 19:52

1 Answers1

0

You should just delete the code and start again.

This might work:

void OnCollisionStay2D(Collision2D collision)
{
    if (collision.gameObject.tag == "Player")
    {
        if (canTakeDmg)
        {
            TakeDamage();
        }
    }



    void TakeDamage()
    {
        var healthComponent = collision.gameObject.GetComponent<PlayerManager>();
        yield return new WaitForSecondsRealTime(3);
        healthComponent.GetDamage(1);
    }
}
gbe
  • 1,003
  • 1
  • 7
  • 23