我希望Unity 4.6中的RectTransform(面板)遵循worldObject。我完成了这项工作,但动作并不如我所愿。当我开始移动相机时,它似乎有些参差不齐,并且落后了。
Vector2 followObjectScreenPos = Camera.main.WorldToScreenPoint (planet.transform.position);
rectTransform.anchoredPosition = new Vector2 (followObjectScreenPos.x - Screen.width / 2, followObjectScreenPos.y - Screen.height / 2);
Run Code Online (Sandbox Code Playgroud)
提示和技巧非常感谢。:-)
有很多选择:
1)您可以将gui画布添加到worldObject并使用此画布渲染面板(只需将其作为子对象添加),但这可能并不是您所需要的。
2)要消除锯齿状运动,您应该以一种或另一种方式补间。DOTween是我的个人喜好,其中以下几行将为您提供所需的结果:
Tweener tweener = transfrom.DOMove (Target.position, 1).SetSpeedBased();
tweener.OnUpdate (() => tweener.ChangeEndValue (Target.position, true));
Run Code Online (Sandbox Code Playgroud)
3)如果您不想在代码中包含依赖项,则可以在Update函数中在当前位置和所需位置(或您的情况下-anchoredPosition)之间执行线性插值。
我建议使用补间,以免使您的更新功能混乱,并且补间通常在各种游戏中都有潜在的用途。
下面是用于线性插值的代码示例,以防您不想使用补间库:
float smoothFactor = 1.0f; //used to sharpen or dull the effect of lerp
var newPosition = new Vector3(x,y,z);
var t = gameObject.transform;
t.position = Vector3.Lerp (t.position,
newPosition,
Time.deltaTime * smoothFactor);
Run Code Online (Sandbox Code Playgroud)
将其放在Update函数中,它将使gameObject遵循特定的newPosition。