【问题标题】:Unity game 2D sidescroller health bar C#Unity 游戏 2D 横向卷轴健康栏 C#
【发布时间】:2014-04-21 13:28:06
【问题描述】:

我是编码新手,我一直在使用 C# 在 Unity 中制作 2D 横向卷轴游戏。我正在尝试添加一个由 5 颗心组成的生命条,半颗心 = 10% 的生命值(附有生命值条的图片)。 .

我在网上找了好几个小时,试图弄清楚如何将这种类型的健康条编码到我的游戏中,但我找不到任何东西。我要先制作 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
            }
        }
    }
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

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

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

    使用透明着色器,例如

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

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

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

    唯一剩下的就是把它放在右边

    【讨论】:

    • 如何将此代码连接到播放器和健康脚本?
    • mmh 我现在只能看到你的健康脚本两次。您将这样做的方式是在脚本的 Start() 方法中创建可以访问健康数据的网格。然后每次运行状况发生变化时,您都会调用方法 updateHealthGUI() 或根据当前运行状况处理顶点变化的方法。
    • 同样创建程序网格一开始似乎有点让人不知所措,只需尝试在开始方法中创建一个四边形,然后玩一会儿,直到你感觉舒服为止。还有一个用于程序网格的统一示例项目的官方包,非常有用。编辑:u3d.as/content/unity-technologies/procedural-examples/3zu
    猜你喜欢
    • 1970-01-01
    • 2017-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多