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;
}