简体   繁体   English

Unity 2D,C#-为什么我的OnCollisionEnter2D不碰撞?

[英]Unity 2D, C# - Why my OnCollisionEnter2D doesn't collide?

So, i created two scripts, one named "Stats.cs" registers the player stats and the other one named "PlayerHealth.cs" "makes" the player take damage on contact and updates the Hearts in the HUD. 因此,我创建了两个脚本,一个名为“ Stats.cs”的脚本注册了玩家的统计信息,另一个名为“ PlayerHealth.cs”的“脚本”使玩家在接触时受到伤害并在HUD中更新了Hearts。 My problem is, whenever i collide with an object that has a tag named "Projectile" it simply doesn't work, my player doesn't take damage at all. 我的问题是,每当我与带有标签“ Projectile”的对象碰撞时,它根本就无法工作,我的播放器根本不会受到伤害。 The Stats.cs script isn't in any object, the PlayerHealth.cs is in my Player object. Stats.cs脚本不在任何对象中,PlayerHealth.cs在我的Player对象中。

Stats.cs Stats.cs

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

[System.Serializable]
public class Stats{

private int health;

public int maxHP = 3;

public int Health
{
    get
    {
        //Some code
        return health;
    }
    set
    {
        //Some code
        health = Mathf.Clamp(value, 0, maxHP);
    }
}
public void SetHealth()
{
    Health = maxHP;
}
}

PlayerHealth.cs PlayerHealth.cs

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

public class PlayerHealth : MonoBehaviour
{
Stats playerStats = new Stats();

public int curHealth;
public int numOfHearts = 3;

public Image[] hearts;
public Sprite fullHeart;
public Sprite emptyHeart;

void OnCollisionEnter2D(Collision2D other)
{
    if (other.gameObject.CompareTag("Projectile"))
    {
        Debug.Log("Hello");
        DamagePlayer(1);
        Destroy(other.gameObject);
    }
}

public void DamagePlayer(int damage)
{
    playerStats.Health -= damage;
}

// Start is called before the first frame update
void Start()
{
    playerStats.SetHealth();
    curHealth = numOfHearts;
}

// Update is called once per frame
void Update()
{

    curHealth = playerStats.Health;
    numOfHearts = playerStats.maxHP;

    if (curHealth>numOfHearts){
        curHealth = numOfHearts;
    }
    if(curHealth <= 0){

        Die();

    }

    for (int i = 0; i < hearts.Length; i++)
    {
        if(i < curHealth){
            hearts[i].sprite = fullHeart;
        } else
        {
            hearts[i].sprite = emptyHeart;
        }

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

    }
}

void Die(){
    //Restart
    Application.LoadLevel(Application.loadedLevel);
}


}

curHealth is updating so it will stay as the actual Health in Stats and will change the images in HUD. curHealth正在更新,因此它将保留为Stats中的实际Health,并会更改HUD中的图像。

The Player has a RigidBody2D on him two colliders, one is a box for the body, and the other is a circle collider, so when the player crouches, the circle collider disables. 玩家在他的两个对撞机上都具有RigidBody2D,一个是用于身体的盒子,另一个是圆形对撞机,因此当玩家蹲下时,圆形对撞机将禁用。

The Projectiles also have and RigidBody2D with 0 gravity (so it won't fall in mid air) and a BoxCollider2D. 投射物还具有重力为0的RigidBody2D(这样它就不会掉到空中)和BoxCollider2D。

I would check and make sure that the projectile is tagged as Projectile and that the BoxCollider doesn't have "Is Trigger" checked. 我将检查并确保将弹丸标记为“弹丸”,并且BoxCollider没有选中“触发”。

I should also say, iterating with that for loop in the Update is very bad practice performance wise. 我也应该说,在Update中用for循环进行迭代是非常糟糕的做法。 That is happening literally as fast as the machine can loop it and it is doing that every time . 实际上,这发生在机器可以循环的速度上,并且每次都在这样做。 I would look into updating it on an event. 我会考虑在事件中进行更新。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM