统一,如何工作RectTransformUtility.ScreenPointToLocalPointInRectangle()函数

时间:2016-08-26 19:26:32

标签: c# unity3d

我正在尝试遵循这个“教程”: http://forum.unity3d.com/threads/ui-follow-scene-objects.364143/

但该家伙说应该使用RectTransformUtility.ScreenPointToLocalPointInRectangle

我试着这样做:

Vector2 localPoint;     

RectTransformUtility.ScreenPointToLocalPointInRectangle(theRectTransformOfMyImage, transform.position, theCamera, out localPoint);

但是图像会转到角落屏幕,而不是在对象中......

任何想法?

编辑:

更多信息,我有一个3D空间,它是一个具有以下健康条形象的敌人场景对象:

在脚本中,变量是:

private Camera _camera;
  private RectTransform _healthBarReact;

  ...


  void Update()
  {
    // it works more or less, with the problem of "the UI element move at half the speed"
    Vector2 screenPoint = RectTransformUtility.WorldToScreenPoint(_camera, transform.position);
    _healthBarReact.position = screenPoint;

    // this doesn't work, the healthBar images goes to the screen corner
    Vector2 localPoint;
   RectTransformUtility.ScreenPointToLocalPointInRectangle(_healthBarReact, screenPoint, _camera, out localPoint);
    _healthBarReact.position = localPoint;
  }

脚本附加到敌人游戏对象

0 个答案:

没有答案