[英]How to freeze rotation in the z & y axes directions? Unity3D
我有一個平台,如果物體分布不均勻,它可以在寬圓柱體上運行 swing。
為此,我創建了一個帶有網格對撞機的圓柱體,在其頂部放置了一個中心彼此重合的平台。
然后我給平台貼了個代碼凍結它的position,就是平台現在只能旋轉,不能掉落。
這是代碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DeterminantOfPlatformEquilibrium : MonoBehaviour
{
Vector3 newYPos;
void Start()
{
newYPos = transform.position;
}
void Update()
{
/*We're setting the position of the object in the
"y" direction - unchanged, and equal to zero,
so that the platform does not fall becaude of gravity*/
newYPos.y += 0.0f;
transform.position = newYPos;
newYPos += new Vector3(0f, 0f, 0f);
}
}
除了凍結平台的 position 之外,我還必須凍結它在 y 和 z 方向上的旋轉,以便它只在一個方向上旋轉 - 在 x 方向。
我嘗試使用從四元數 class 調用 Euler() 方法來做到這一點,將 x 軸設置一定的旋轉角度,並將其他 2 個軸設置為零,在我看來一切都應該有效,但是這一行在我的代碼中似乎沒有做任何事情: Quaternion rotation = Quaternion.Euler(23f, 0f, 0f);
您是否有更多可能的選項來凍結這兩個軸?
凍結所有旋轉
rigidbody.constraints = RigidbodyConstraints.FreezeRotation;
凍結特定旋轉:
rigidbody.constraints = RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
你可以找到查看Unity Documentation ,但他們並沒有很好地解釋它
我看到你是 Unity3D 的新手。 你的“位置凍結”實際上什么都不做,你只是在位置上加零,而在某些東西上加零不會改變它。 如果您的圓柱體沒有Rigidbody
組件,它就不會因重力而下落。
您的旋轉對您沒有任何作用,因為您創建了一個名為rotation
的變量,但您沒有將其分配給任何地方。 此外,您可以將Vector3.right
乘以 X 值,而不是為 Y 和 Z 寫入零。 所以在 X 方向旋轉你的圓柱體的代碼是:
transform.rotation = Quaternion.Euler(Vector3.right * 23f);
我不確定我是否沒有誤解你的問題,但是這行代碼:
Quaternion rotation = Quaternion.Euler(23f, 0f, 0f);
什么都不做,因為您從不使用您創建的變量。 如果你想設置旋轉嘗試:
transform.rotation = Quaternion.Euler(...)
如果您使用的是剛體,您還可以在檢查器中限制圍繞特定軸的旋轉。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.