使UI元素跟随摄像机

问题描述 投票:0回答:1

我想使菜单跟随摄像机,因此当用户凝视菜单时,可以触发菜单。

我参考FirstPersonCamera功能中的主相机Update来设置菜单的位置,

menuCanvas.transform.position = FirstPersonCamera.transform.position 
                               +(FirstPersonCamera.transform.forward * 4);

不幸的是,菜单始终位于相机中心,因此始终会被触发。如何正确放置上下文菜单?

c# unity3d menu virtual-reality
1个回答
1
投票

您可以使用Vector3.Lerp进行移动[[平滑(对于较远距离的对象,运动较快,对于较短距离的对象,运动较慢)。

然后,您可以将目标位置限制为不在显示器的中央,而是在每个方向上都可以偏移。一个非常简单的示例可能看起来像

// how far to stay away fromt he center public float offsetRadius = 0.3f; public float distanceToHead = 4; public Camera FirstPersonCamera; // This is a value between 0 and 1 where // 0 object never moves // 1 object jumps to targetPosition immediately // 0.5 e.g. object is placed in the middle between current and targetPosition every frame // you can play around with this in the Inspector [Range(0, 1)] public float smoothFactor = 0.5f; private void Update() { // make the UI always face towards the camera transform.rotation = FirstPersonCamera.transform.rotation; var cameraCenter = FirstPersonCamera.transform.position + FirstPersonCamera.transform.forward * distanceToHead; var currentPos = transform.position; // in which direction from the center? var direction = currentPos - cameraCenter; // target is in the same direction but offsetRadius // from the center var targetPosition = cameraCenter + direction.normalized * offsetRadius; // finally interpolate towards this position transform.position = Vector3.Lerp(currentPos, targetPosition, smoothFactor); }

当然,这远非完美,但我希望这是一个很好的起点。


moe复合物/完全溶液可以例如在MixedRealityToolkit中找到:SphereBasedTagalong

SphereBasedTagalong
© www.soinside.com 2019 - 2024. All rights reserved.