Unity C#loops:心脏健康酒吧
我有问题,如果currentHealth是25或其倍数(50的倍数除外),我想在空栏中添加一个半心图像,因为如果currentHealth是例如50,我想在半心图像以前所在的栏中显示我的全心图像。我使用了if IUnity C#loops:心脏健康酒吧,c#,unity3d,C#,Unity3d,我有问题,如果currentHealth是25或其倍数(50的倍数除外),我想在空栏中添加一个半心图像,因为如果currentHealth是例如50,我想在半心图像以前所在的栏中显示我的全心图像。我使用了if I=nextatcktime) { if(Input.GetKeyDown(KeyCode.Mouse0)) { 攻击(); nextAttackTime=Time.Time+1f/攻击率; } } for(int i=0;i
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerCombat : MonoBehaviour
{
public int maxHealth = 600;
public int currentHealth;
public int numOfHearts;
public Transform attackPoint;
public LayerMask enemyLayers;
public float attackRange = 0.5f;
public int attackDamage = 50;
public float attackRate = 2f;
float nextAttackTime = 0f;
public Image[] hearts;
public Sprite fullHeart;
public Sprite halfHeart;
public Sprite emptyHeart;
void Start()
{
currentHealth = maxHealth;
}
void Update()
{
if (Time.time >= nextAttackTime)
{
if (Input.GetKeyDown(KeyCode.Mouse0))
{
Attack();
nextAttackTime = Time.time + 1f / attackRate;
}
}
for (int i = 0; i < hearts.Length; i++)
{
if (i < currentHealth / 50)
{
hearts[i].sprite = fullHeart;
if (currentHealth % 50 == 0 - 25)
{
}
}
else
{
hearts[i].sprite = emptyHeart;
}
if (i < numOfHearts)
{
hearts[i].enabled = true;
}
else
{
hearts[i].enabled = false;
}
}
}
void Attack()
{
Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayers);
foreach (Collider2D enemy in hitEnemies)
{
enemy.GetComponent<Enemy>().TakeDamage(attackDamage);
}
}
void OnDrawGizmosSelected()
{
if (attackPoint == null)
return;
Gizmos.DrawWireSphere(attackPoint.position, attackRange);
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
公共类玩家Combat:单一行为
{
公共int maxHealth=600;
公共卫生;
公众心;
公共转换攻击点;
公共层屏蔽层;
公众浮标攻击范围=0.5f;
公共int攻击伤害=50;
公共浮动攻击率=2f;
float nextatcktime=0f;
公众形象,;
公众精神饱满;
公共精灵半心;
公共精灵空心;
void Start()
{
currentHealth=maxHealth;
}
无效更新()
{
如果(Time.Time>=nextatcktime)
{
if(Input.GetKeyDown(KeyCode.Mouse0))
{
攻击();
nextAttackTime=Time.Time+1f/攻击率;
}
}
for(int i=0;i
您可以设置图像。键入至填充,然后将填充方法设置为水平。将锚定设置在左侧,以便从左向右填充。然后使用代码中的image.fillAmount
指定图像的填充量
这允许显示心脏的分数
fillAmount
是从0到1的浮动,0表示未填充,1表示完全填充
代码上下文中的示例可能类似于
var remainingHeartsHealth = myCurrentHealth;
foreach (var heartImage in heartImages)
{
// Get the health value for this heart, max of 50
//
var thisHeartsHealth = Mathf.Min(remainingHeartsHealth, 50);
// Normalize the hearts health to be between 0 and 1
//
heartImage.fillAmount = thisHeartsHealth / 50f;
// Update the remaining health to be used for hearts,
// subtracting the amount we just used, but making sure to not go below 0
//
remainingHeartsHealth = Mathf.Max(remainingHeartsHealth - thisHeartsHealth, 0);
}
请使用正确的标签unityscript
是早期Unity版本中使用的一种类似JavaScript风格的自定义语言,现在已经被长期弃用了!