Unity游戏2D sidescroller健康栏C#

时间:2014-04-21 05:10:24

标签: c# unity3d

我是编码的新手,我一直在使用C#在Unity中制作2D sidescroller游戏。我正在尝试添加一个由5颗心,半颗心=健康的10%组成的健康栏(附有健康栏的图片)。 enter image description here  。 enter image description here

我在网上看了好几个小时试图弄清楚如何将这种类型的健康栏编码到我的游戏中,但我找不到任何东西。我先制作一个GUItexture吗?代码会是什么样的?我想要它,所以每当玩家受到10点伤害时,健康棒就会失去一半的心脏。我应该将什么添加到我现有的脚本中,并且必须创建一个新脚本?

无论如何,将不胜感激,谢谢。

这是我的播放器代码......

      using UnityEngine;

/// <summary>
/// Handle hitpoints and damages
/// </summary>
public class HealthScript : MonoBehaviour
{
    /// <summary>
    /// Total hitpoints
    /// </summary>
    public int hp = 1;

    /// <summary>
    /// Enemy or player?
    /// </summary>
    public bool isEnemy = true;

    /// <summary>
    /// Inflicts damage and check if the object should be destroyed
    /// </summary>
    /// <param name="damageCount"></param>
    public void Damage(int damageCount)
    {
        hp -= damageCount;

        if (hp <= 0)
        {
            // 'Splosion!
            SpecialEffectsHelper.Instance.Explosion(transform.position);

            // Dead!
            Destroy(gameObject);
        }
    }

    void OnTriggerEnter2D(Collider2D otherCollider)
    {
        // Is this a shot?
        ShotScript shot = otherCollider.gameObject.GetComponent<ShotScript>();
        if (shot != null)
        {
            // Avoid friendly fire
            if (shot.isEnemyShot != isEnemy)
            {
                Damage(shot.damage);

                // Destroy the shot
                Destroy(shot.gameObject); // Remember to always target the game object, otherwise you will just remove the script
            }
        }
    }
}

这是我的健康状况

 using UnityEngine;

/// <summary>
/// Handle hitpoints and damages
/// </summary>
public class HealthScript : MonoBehaviour
{
    /// <summary>
    /// Total hitpoints
    /// </summary>
    public int hp = 1;

    /// <summary>
    /// Enemy or player?
    /// </summary>
    public bool isEnemy = true;

    /// <summary>
    /// Inflicts damage and check if the object should be destroyed
    /// </summary>
    /// <param name="damageCount"></param>
    public void Damage(int damageCount)
    {
        hp -= damageCount;

        if (hp <= 0)
        {
            // 'Splosion!
            SpecialEffectsHelper.Instance.Explosion(transform.position);

            // Dead!
            Destroy(gameObject);
        }
    }

    void OnTriggerEnter2D(Collider2D otherCollider)
    {
        // Is this a shot?
        ShotScript shot = otherCollider.gameObject.GetComponent<ShotScript>();
        if (shot != null)
        {
            // Avoid friendly fire
            if (shot.isEnemyShot != isEnemy)
            {
                Damage(shot.damage);

                // Destroy the shot
                Destroy(shot.gameObject); // Remember to always target the game object, otherwise you will just remove the script
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我可能会使用我在这样的脚本中创建的四边形(网格):

http://docs.unity3d.com/Documentation/Manual/Example-CreatingaBillboardPlane.html

使用像

这样的透明着色器

http://docs.unity3d.com/Documentation/Components/shader-TransCutDiffuse.html

并在您失去健康时将最右侧的顶点编辑到您想要的位置。因此,如果您的网格对于5个心脏是10个单位长度,那么4.5个心脏将是9个单位长度。编辑顶点数据的示例是示例2或3:

http://docs.unity3d.com/Documentation/ScriptReference/Mesh.html

唯一剩下的就是正确定位