【问题标题】:Rotate object by 90 degrees?将物体旋转 90 度?
【发布时间】: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


【解决方案1】:

所以我认为目前存在多个问题/不清楚的事情

  • 您正在围绕90° 旋转每0.1 秒,因此在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 Pooling

【讨论】:

  • 感谢您的回答!它仍然以某种方式不起作用,平台没有旋转。是的,确实产生了很多平台。我愿意解决它,所以每个输入生成一个平台。
【解决方案2】:

我设法找到了解决问题的方法。也许对某人有帮助,所以你去吧:

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 让我开始了解如何做到这一点!

【讨论】:

  • Step 什么时候被调用?目前尚不清楚这与问题中的代码有何联系
【解决方案3】:

您可以使用它,它来自 Unity3D 文档。

https://docs.unity3d.com/ScriptReference/Transform-eulerAngles.html

【讨论】:

  • 这不是我该工作所需要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-22
  • 2015-03-22
  • 2011-09-24
  • 2020-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多