嘗試實現(xiàn)基于垂直軸和水平軸統(tǒng)一旋轉(zhuǎn)對象的方法,垂直軸和水平軸由光標(biāo)在圖像上的位置確定。使用操縱桿進行控制,為移動設(shè)備創(chuàng)建 3D 游戲。目的是使用操縱桿進行旋轉(zhuǎn)。圖片示例: https ://imgur.com/a/hd9QiVe 綠色圓圈隨著用戶按下而移動,并返回 -1 到 1 之間的 X 和 Y 值,中間為 0。只是為了可視化輸入是如何發(fā)生的: https ://imgur.com/a/8QVRrIh 如圖所示,我只是想要角度或一種方法來在檢測到用戶輸入的方向上移動對象。嘗試了幾種使用 atan 和 tan 計算角度的方法,但我的數(shù)學(xué)很糟糕,我不完全確定我首先獲取了正確的值。//背景操縱桿指的是第一張圖片中的白色圓圈 pos.x = (pos.x / backgroundJoystick.rectTransform.sizeDelta.x); pos.y = (pos.y / backgroundJoystick.rectTransform.sizeDelta.y); inputVector = new Vector3(pos.x * 2f, 0, pos.y * 2f); inputVector = (inputVector.magnitude > 1.0f) ? inputVector.normalized : inputVector;//抓取軸輸入public float Horizontal(){ if (inputVector.x != 0) { return inputVector.x; } else return Input.GetAxis("Horizontal");}public float Vertical(){ if (inputVector.z != 0) { return inputVector.z; } else return Input.GetAxis("Vertical");}如代碼中所示,角度對于垂直和水平方向的 input.getaxis 是必要的,以將對象指向角度。目前使用的公式不提供任何角度。
1 回答

largeQ
TA貢獻(xiàn)2039條經(jīng)驗 獲得超8個贊
如果要獲取矢量的角度,請使用Vector2.SignedAngle()
:
var inputAngle = Vector2.SignedAngle(Vector2.right, inputVector);
角度是相對的,這就是為什么需要指定Vector2.right
為第一個參數(shù)的原因。還有一種Vector2.Angle()
方法,但它只返回兩個角度之間的距離,并沒有考慮方向。
如果您需要驗證您的輸入向量是否如您所想,請使用Debug.Log()
打印您的inputVector
.
- 1 回答
- 0 關(guān)注
- 333 瀏覽
添加回答
舉報
0/150
提交
取消