摘要:本文原创,转载请注明出处:http://www.cnblogs.com/AdvancePikachu/p/6733620.html
需求:
类似NPC血条,当NPC处于摄像机视野内,血条绘制,且一直保持在NPC头顶。
开始:
网上查找资料,然后编写代码:
public RectTransform rectBloodPos; void Update () { this.gameObject.transform.Translate (Input.GetAxis ("Horizontal") * 10 * Time.deltaTime, 0, 0); this.gameObject.transform.Translate (0, 0, Input.GetAxis ("Vertical") * 10 * Time.deltaTime); Vector2 vec2 = Camera.main.WorldToScreenPoint (this.gameObject.transform.position); rectBloodPos.anchoredPosition = new Vector2 (vec2.x - Screen.width / 2 + 0, vec2.y - Screen.height / 2 +