我以前和我有統一的知識,如果對象是在IMU數據作爲四元數讀取與IMU的工作,那麼你就不會遇到gimbo鎖問題。因此,您確實可以創建對象旋轉的引用並將其轉換爲euler天使,然後在將其應用到對象之前轉換回Quaternion。
但是,如果你只是想限制旋轉,我會做這樣的事情:
//the last rotation was pointed at the grey zone.
private Quaternion prevAllowedRotation;
void FixedUpdate(){
if(!isValidRotation()){
this.transform.rotation = prevAllowedRotation;
}else{
this.transform.lookAt(lockToArea());
}
}
private bool isValidRotation(){
//I chose forward based on the image, find the direction that works for you
Ray ray = new Ray(this.transform.position, this.transform.forward);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 10f){
if(hit.transform.tag == "wall"){
return true;
}
}
return false;
}
private Vector3 lockToArea(Gameobject grey){
Vector3 center = grey.transform.position;
//figure out how to get the position of the facing direction on the same plane as the grey area.
Vector3 pointerPosition = getPointerPosition();
RaycastHit hit;
if(Physics.Linecast(pointerPosition, center, hit)){
return hit.point;
}
return Vector3.zero;
}
private Vector3 getPointerOnPlane(){
Ray ray = new Ray(this.transform.position, this.transform.forward);
// you need to figure out how to dyanmically get the distance so that the ray lands on the same plane as the vector
float distance = 0;
return ray.GetPoint(distance);
}
這將鎖定其旋轉僅在灰色的立方體被人指指點點。這應該對你有很大的幫助,你只需要知道IMU數據讀入的位置,並且與灰色立方體的中心在同一平面上。
將'四元數旋轉= Quaternion.Euler(新的Vector3(transform.eulerAngles.x,0,0)) ;'適用? –
感謝@Peh的版本。 謝謝瑞安,但我想避免歐拉角,因爲原來的四元數來自外部IMU,並限制歐拉角的方向我有一些麻煩 – javinair
[Quaternion.eulerAngles](https://docs.unity3d.com /ScriptReference/Quaternion-eulerAngles.html)是一件事,但我懷疑這就是你已經在使用。我知道'wxyz'的價值根本無濟於事,因爲它們不完整。我能想到的最好的方法是在兩個藍色框中計算一個歐拉'AngleTo()'(你必須寫這個方法),並檢查它的X和Y分量都大於0(或者小於)。 – Draco18s