【发布时间】:2022-01-13 09:26:30
【问题描述】:
我知道这已经被回答了 1000 次,但我只是不知道我应该如何编码。 我想要的是当平台改变它在 x 或 z 轴上的位置时,然后将整个平台旋转 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
}
}
感谢所有帮助!
【问题讨论】:
-
我看不到你在哪里旋转任何东西......
-
因为我从中删除了。我尝试在随机 if 语句中旋转,所以当平台位置发生变化时,它也会旋转。但它没有。
-
请展示你的尝试;)
-
我编辑了帖子,请看一下。可能很可怕。
标签: c# android unity3d object rotation