简体   繁体   English

如何在 Y 轴上旋转对象?

[英]How do I rotate an object on the Y axis?

I'm trying to make a game with a 'rotating' Earth, but I don't know how to rotate... This is what I've got so far, help would be much appreciated:我正在尝试使用“旋转”地球制作游戏,但我不知道如何旋转......这是我到目前为止所得到的,非常感谢帮助:

using UnityEngine;

public class Earth_Rotation : MonoBehaviour
{
     // Update is called once per frame
     void Update()
     {
     Transform.Rotate (0, 10, 0);
     }
}

You need to call the Rotate() method on your objects transform component.您需要在对象转换组件上调用Rotate()方法。 Uppercase Transform refers to the Transform class itself where lowercase transform refers to this objects instance of a transform component.大写Transform指的是Transform类本身,其中小写transform指的是转换组件的这个对象实例。 If you want to manipulate the object your script is attached to you need lowercase transform .如果你想操作你的脚本所附加的对象,你需要小写transform I suggest checking out this link to learn about classes and objects: Class and Object - GeeksforGeeks我建议查看此链接以了解类和对象: Class and Object - GeeksforGeeks

Further, if you want your object to turn over time, you need to reference the time somewhere.此外,如果您希望您的对象随时间推移,您需要在某处引用时间。 This can be achieved via Time.deltaTime which returns the time passed since the last frame in Unity.这可以通过Time.deltaTime实现,它返回自 Unity 中的最后一帧以来经过的时间。 Try something like this:尝试这样的事情:

void Update()
{
    //Vector3.up is a vector that looks like this: (0,1,0)
    transform.Rotate(Vector3.up * Time.deltaTime);
}

You can also add a modifier like public float turnSpeed and multiply by that to increase or decrease your objects turn speed:你还可以添加一个像public float turnSpeed这样的修改器,然后乘以它来增加或减少你的对象的转动速度:

public float turnSpeed;

void Update()
{
    transform.Rotate(Vector3.up * Time.deltaTime * turnSpeed);
}

If you set turnSpeed = 10 you have your original value.如果您将turnSpeed = 10设置turnSpeed = 10原始值。

Always take a look at the documentation for the functions you are trying to use.请务必查看您尝试使用的功能的 文档 It helps a lot to understand how and where to use them.了解如何以及在何处使用它们有很大帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM