[英]Unity game 2D sidescroller health bar C#
我是编码的新手,我一直在使用C#在Unity中制作2D Sidecroller游戏。 我正在尝试添加由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
}
}
}
}
我可能会使用在如下脚本中创建的四边形(网格):
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
剩下的唯一一件事就是将其正确定位
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.