【问题标题】:How to fix mirroring of canvas in unity如何统一修复画布的镜像
【发布时间】:2020-08-26 07:21:39
【问题描述】:

我有一个简单的问题:如何避免翻转附在主角身上的血条?我不知道该怎么做。项目可以在这里下载:https://mega.nz/file/zUY0HYbL#ahwM_uGlp7-5iMLFjR1uaj6hgeVpjyhB3SCLe9xAt88

抱歉,我无法在此处附上。

【问题讨论】:

  • 答案已准备就绪

标签: unity3d mirroring


【解决方案1】:

只要你有一个单一的健康栏值,比如一个决定健康栏大小的浮点数,你可以简单地在void Update()函数中添加这一行,它会修复健康栏翻转:

health = Mathf.Clamp(health, 0, maxHealth);

其中 health 是决定生命条长度和生命条比例的值。 (对不起,如果我只是不知道你在使用什么变量,你应该在我下次推荐时包含一段代码)并且 ma​​xHealth 是最大玩家健康或健康条的最大比例。

[编辑] 把这行代码放到你的 Update 函数中。

currentHealth = Mathf.Clamp(currentHealth, 0, maxHealth);

简单地说,它把 currentHealth 的值固定在 0 和 ma​​xHealth 之间 所以它不能高于 ma​​xHealth 并低于零。

祝你的项目好运。 :D

【讨论】:

  • 顺便说一句,整个画布翻转,所以谢谢你的回答,但它没有帮助。
  • 哦。我认为您正在为您的角色应用这个翻转代码到您的画布或画布中的所有内容。检查您是否在代码中不小心将错误的游戏对象设置为变量。您可能已经添加了显示玩家健康状况的 ui。它不会翻转整个 Cancas,因为尽管操纵杆仍处于相同位置
【解决方案2】:

你来了!

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

public class Player : MonoBehaviour
{

    public int maxHealth = 100;
    public int currentHealth;

    public Healthbar healthBar;

    // Start is called before the first frame update
    void Start()
    {
        currentHealth = maxHealth;
        healthBar.SetMaxHealth(maxHealth);
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            TakeDamage(20);
        }
    }

    void TakeDamage(int damage)
    {
        currentHealth -= damage;

        healthBar.SetHealth(currentHealth);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-31
    • 1970-01-01
    • 2021-03-30
    • 2014-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多