简体   繁体   中英

How do I get an object to rotate on only one axis?

I'm using Unity 5 (latest version), and I'm trying to make a conveyor belt type of thing. To do so I want to have cylinders rotate on the z-axis, and only the z-axis. How would I do that?

You can use the transform.Rotate method to rotate an object around a fixed axis.

The method has various constructors but a simple way to achieve what you want would be using the following depending on the axis you actually want to rotate the object around.

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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