c# - 如何限制 Quaternion.Slerp() 函数的旋转
问题描述
我一直在做一个 VR 项目,其中一双眼睛跟踪世界空间中的一个点。对于初学者,我使用玩家的控制器作为目标。眼睛目前完全按照我的预期跟踪,但是由于它们是眼睛,我想限制它们可以向上/向下和向左/向右看的距离。我曾尝试使用 localEulerAngles 来完成此操作,这似乎可行,但是一旦我输入“错误” if block,眼睛旋转就不会再改变,因此会卡住。所以我认为我需要强制旋转回到可接受的限制,但我不知道如何正确地做到这一点。我还认为可能有更好的方法来完成我不知道的整件事。这是我当前的更新功能:
void Update(){
Vector3 direction = finger.position - this.transform.position;
float angleX = transform.localEulerAngles.x;
//this text is displayed in VR so that I can see while I test
temp.text = transform.localEulerAngles.x.ToString();
//this condition is for if the finger is held up in the air
if (pointing)
{
//because of the way angles work, it is necessary to check what CANT be?.
if (angleX > 15 && angleX < 350)
{
//what to do here?
}
else
{
//look at the finger
this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(direction), 0.1f);
}
}
}
解决方案
使用该Mathf.Clamp
方法将浮点值限制在最小值和最大值之间,然后将变换的 X 角度重置为钳制值。
所以你的Update
方法变成:
void Update() {
Vector3 direction = finger.position - this.transform.position;
float angleX = transform.localEulerAngles.x;
//this text is displayed in VR so that I can see while I test
temp.text = transform.localEulerAngles.x.ToString();
//this condition is for if the finger is held up in the air
if (pointing)
{
float clampedAngle = Mathf.Clamp(angleX, 15f, 350f);
transform.localEulerAngles.x = clampedAngle;
//look at the finger
this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(direction), 0.1f);
}
}
推荐阅读
- javascript - 在AngularJS中将嵌入SVG的动态和复杂的html代码转换为PDF
- flutter - Firebase 身份验证!即使我输入非注册用户,我也可以移动到主页
- css - 是否有另一种方法可以使列布局更好地响应?
- java - HTTP GET REQUEST (HTTPURLCONNECTION) set-cookie null (NOT HTTPONLY COOKIE)
- javascript - 如何将数组中的数组拆分为json?地图 Javascript
- laravel - 改变字体粗细也改变 font-family Laravel--dompdf
- makefile - 当通配符是字符串的一部分时,Makefile 中的通配符目标
- java - Itext 7 PDF - 将图像添加到文档不会刷新到磁盘,但会保留在内存中,这与 Itext 5 不同
- laravel - Illuminate\Contracts\Container\BindingResolutionException .Target 类 [MarketsouhaibController] 不存在。拉拉维尔 8
- flutter - 被 18 像素抖动溢出