首页 > 解决方案 > 如何使用 Unity 的 Update 函数指定对象的旋转值

问题描述

我想在 Unity 中指定对象旋转值。

我不想旋转对象。

我写了以下代码。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

    public class test_rotate : MonoBehaviour {

        int test_x = 10;

        // Use this for initialization
        void Start () {

        }

        // Update is called once per frame
        void Update () {
            this.transform.Rotate(test_x, 0, 0);
        }
    }

然后对象开始旋转。

在此处输入图像描述

这不是我想要的。

我只想将对象的旋转值设置为(10,0,0)。

我还尝试了以下代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test_rotate : MonoBehaviour {

    int test_x = 10;

    // Use this for initialization
    void Start () {
        this.transform.Rotate(test_x, 0, 0);
    }

    // Update is called once per frame
    void Update () {
    }
}

然后它按预期工作,但我想用 Update 函数而不是 Start 函数来做到这一点。

我该怎么办?

标签: c#unity3d

解决方案


transform.Rotate()每次调用时都会旋转对象。

你想要做的是设置变换的旋转:transform.rotation = Quaternion.Euler(test_x, 0, 0);


推荐阅读