[英]How do I get an object to rotate on only one axis?
我正在使用Unity 5(最新版本),并且正在尝试制作传送带类型的东西。 为此,我希望圆柱体在z轴上旋转,并且仅在z轴上旋转。 我该怎么做?
您可以使用transform.Rotate
方法绕固定轴旋转对象。
该方法具有各种构造函数,但根据实际旋转对象的轴,使用以下方法可以轻松实现所需的目标。
using UnityEngine;
using System.Collections;
public class RotateCylinder : MonoBehaviour
{
// rotation speed in degrees per second.
private float rotationSpeed = 1f;
void Update()
{
// Use one of the following depending on the axis you want to rotate the object, this will depend on how your object is transformed.
// Rotate around X Axis
transform.Rotate(Vector3.right * rotationSpeed * Time.deltaTime);
// Rotate around Y Axis
transform.Rotate(Vector3.up * rotationSpeed * Time.deltaTime);
// Rotate around Z Axis
transform.Rotate(Vector3.forward * rotationSpeed * Time.deltaTime);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.