[英]Rotate object by 90 degrees?
我知道這已經被回答了 1000 次,但我只是不知道我應該如何編碼。 我想要的只是當平台在 x 或 z 軸上更改其 position 時,然后將整個平台旋轉 90 度。 我嘗試使用 platform.transform.Rotate(0, 90, 0),所以我認為還有更多工作要做。 代碼本身:
public GameObject platform;
public Transform lastPlatform;
Vector3 lastPosition;
Vector3 newPos;
bool stop;
private Quaternion rotationQuaternion;
void Start()
{
lastPosition = lastPlatform.position;
StartCoroutine(SpawnPlatforms());
rotationQuaternion = transform.rotation;
}
void Update()
{
}
IEnumerator SpawnPlatforms()
{
while (!stop)
{
GeneratePosition();
Instantiate(platform, newPos, rotationQuaternion * Quaternion.identity);
lastPosition = newPos;
yield return new WaitForSeconds(0.1f);
}
}
void GeneratePosition()
{
newPos = lastPosition;
int rand = Random.Range(0, 2);
if (rand > 0)
{
newPos.x += 1.5f;
transform.rotation = rotationQuaternion * Quaternion.Euler(0, 90, 0); //one way i tried
}
else
{
newPos.z += 1.5f;
platform.transform.Rotate(0, 90, 0) //another way I tried
}
}
我感謝所有的幫助!
所以正如我所看到的,目前有多個問題/不清楚的事情
0.1
秒旋轉約90°
,因此在 1 秒內您旋轉約900°
。 在我看來,這聽起來很簡單。transform
而不是平台platform
似乎是資產中的預制件,因此旋轉它沒有意義你可能想做
void GeneratePosition()
{
newPos = lastPosition;
int rand = Random.Range(0, 2);
if (rand > 0)
{
newPos.x += 1.5f;
}
else
{
newPos.z += 1.5f;
}
// rather directly rotate the quaternion parameter
// if you are going to do the same thing in both cases anyway I would rather extract it right away
rotationQuaternion *= Quaternion.Euler(0, 90, 0);
}
然后也
// Rotating by "Quaternion.idendity" has no effect
Instantiate(platform, newPos, rotationQuaternion);
對我來說這聽起來還是很多,也許你想看看Object 池
我設法找到了解決問題的方法。 也許對某人有幫助,所以這里是 go:
public struct SpawnPoint
{
public Vector3 position;
public Quaternion orientation;
public void Step(float distance)
{
if (Random.value < 0.5)
{
position.x += distance;
orientation = Quaternion.Euler(0, 90, 0);
}
else
{
position.z += distance;
orientation = Quaternion.Euler(0, 0, 0);
}
//orientation = Quaternion.Euler(0, 90, 0) * orientation;
}
}
void Start()
{
_spawn.position = lastPlatform.position;
_spawn.orientation = transform.rotation;
}
從現在開始,剩下要做的就是實例化:
var newPlatform = Instantiate(platform, _spawn.position, _spawn.orientation);
非常感謝 derHugo 讓我開始了解如何做到這一點!
您可以使用它,它來自 Unity3D 文檔。
https://docs.unity3d.com/ScriptReference/Transform-eulerAngles.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.