public class FollowPlayer : MonoBehaviour
{
public float xOffset;
public float yOffset;
public RectTransform recTransform;
void Update()
{
Vector2 player2DPosition = Camera.main.WorldToScreenPoint(transform.position);
recTransform.position = player2DPosition + new Vector2(xOffset, yOffset);
//血条超出屏幕就不显示
if (player2DPosition.x > Screen.width || player2DPosition.x < 0 || player2DPosition.y > Screen.height || player2DPosition.y < 0)
{
recTransform.gameObject.SetActive(false);
}
else
{
recTransform.gameObject.SetActive(true);
}
}
}
挂载主角身上。将相机标签 改为 MainCamera