我想使菜单跟随摄像机,因此当用户凝视菜单时,可以触发菜单。
我参考FirstPersonCamera
功能中的主相机Update
来设置菜单的位置,
menuCanvas.transform.position = FirstPersonCamera.transform.position
+(FirstPersonCamera.transform.forward * 4);
不幸的是,菜单始终位于相机中心,因此始终会被触发。如何正确放置上下文菜单?
您可以使用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