微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

OnCollisionStay 和协程问题

如何解决OnCollisionStay 和协程问题

大家好,我有一个小问题。只要玩家每 X 秒站在平台游戏上,我就试图让平台游戏对玩家造成伤害,现在它可以工作,但只要我继续移动,如果玩家只是站在那里什么都不会发生。

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;
        }
    }
}

玩家运动 - 对于我在玩家运动上做错的情况。

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
        }
    }
}

解决方法

您应该删除代码并重新开始。

这可能有效:

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);
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。