我想旋轉一個物體以匹配它的地面。所以我投射了 3 條光線(在角落)來計算下面飛機的法線。現在我需要相應地旋轉物件,但保持 y 旋轉(所以它“面向”的方向)所以只是設定transform.up = normal不起作用。我想我可以只使用變換方向之間的點積來旋轉它(所以xRotation = Vector3.Dot(normal, transform.forward)對于x和zRotation = Vector3.Dot(normal, transform.right)z)這應該是法線向量和右/前向量之間的角度。但結果我的物件只是面向天空,這樣的想法是完全錯誤的。你知道我應該怎么做嗎?
uj5u.com熱心網友回復:
這是您的問題的解決方案。盡管有不同的方法可以做到這一點,但我個人認為最好使用Cross軸。實際上,您需要Vector3.Cross代替Vector3.Dot. 此代碼的作業方式是通過將玩家的 乘以transform.right地面法線向量,由于該軸計算垂直方向,因此您可以期望它給出與地面相對應的前向。
public LayerMask groundLayer; // The Layer Ground
public Vector3 offset = Vector3.up;
private void Update()
{
if (Physics.Raycast(transform.position, Vector3.down, out var groundHit, 2f, groundLayer.value))
{
var cross = Vector3.Cross(transform.right, groundHit.normal);
var _lookRotation = Quaternion.LookRotation(cross, groundHit.normal);
transform.position = groundHit.point offset; // the offset is OPTIONAL, you can adjust it manuel or remove
transform.rotation = _lookRotation;
}
}
結果:
您可以在下面看到結果。CharacterController考慮您可以洗掉偏移代碼并使用諸如或之類的機制使其與地面兼容Rigidbody。


轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/480076.html
上一篇:二維陣列python的乘積之和
下一篇:這種算術導數的實作有什么問題?
