Unity血条跟随需要注意的地方

1.尽量不要把血条UI设置为怪物的子物体(除非你想怪物转动,血条也跟着转)

2.画布渲染模式设置为 世界空间 ,之后将血条拖到怪物头上的合适位置,定义一个Vector3变量offset记录血条的rect Transform与怪物的Transform差值。

    血条的位置=初始差值+怪物的当前位置

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


public class lifeUI : MonoBehaviour
{

   public Image life; //实际生命
    public Image MaxLife;//最大生命
    public Transform followAbove;//血条要跟随的怪物位置
    Vector3 offset;//偏移量
    
    void Start()
    {
        offset = life.rectTransform.position - followAbove.position;//偏移量=血条初始位置—怪物初始位置
    }
    void Update()
    {

        life.rectTransform.position = followAbove.position + offset;//实际血条位置=怪物的位置+偏移量
        MaxLife.rectTransform.position = followAbove.position + offset;//最大血条位置=怪物的位置+偏移量

     }

}

猜你喜欢

转载自blog.csdn.net/qianhang120/article/details/126377955