Unity血条跟随效果

馋奶兔 提交于 2019-11-30 20:50:28

1、OGUI实现血条跟随

public Camera mainCam;
public Transform inPoint;
private float hSliderValue;
private void OnGUI()
{

//将世界坐标换成屏幕坐标
Vector3 temp = mainCam.WorldToScreenPoint(inPoint.position);
GUI.color = Color.red;
//GUI.Label(new Rect(temp.x, Screen.height-temp.y, 100, 20), "唐三");
hSliderValue = GUI.HorizontalSlider(new Rect(temp.x,Screen.height- temp.y, 100, 30), hSliderValue, 0.0f, 10.0f);
}

2、NGUI实现血条跟随(添加HUD插件)

 

 实现加血减血显示

 

 

public HUDText hud;
public float speed;
void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
transform.position += new Vector3(h * Time.deltaTime * speed, v * Time.deltaTime * speed, 0);
if (Input.GetMouseButtonDown(0))
{
hud.Add("+100",Color.green,0.5f);
}
if (Input.GetMouseButtonDown(1))
{
hud.Add("-100", Color.red, 0.5f);
}
}

脚本加到Cube上控制移动和加减血的显示

3、UGUI血条跟随

 

public Camera mainCam;
public Transform inPoint;

void Update()
{

//将世界坐标换成屏幕坐标
Vector3 temp = mainCam.WorldToScreenPoint(inPoint.position);
temp.x -= Screen.width * 0.5f;
temp.y -= Screen.height * 0.5f;
transform.localPosition = temp;
}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!