Local rotation (763355)

Hi, how could I rotate an object around the local Y-axis? (Something like this)

Please clarify, in the UI or with code? In the UI you can just toggle the Local rotation mode on. It’s up there in the top left corner of Unity UI, next to the tool-handle mode toggle.

In code you can use Transform.localRotation.

I tried to use Transform.localRotation but I don’t understand how it works, usually I use transform.Rotate()

Make a simple test. Parent a cube to another, maybe offset it so that you can see it easy.
Apply this code to the child cube, put it to Start so that it runs once:

transform.localRotation = Quaternion.Euler(45.0f, 0.0f, 0.0f);

I know many people advise against using Euler rotation but that’s one way to do it in local space. Now your cube is rotated 45 degrees on it’s local x-axis.

1 Like

Maybe an animation would be more simple?

Well. It depends on what you are trying to achieve. That code can’t get much simpler.

I need to rotate the object every frame

Well then just do it in Update incrementally or whatever method you run to do your update.

This is on update

@Tornado77 try something simple like this:

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

public class RotateLocal : MonoBehaviour
{
    public float angle = 0f;
    public float rotationSpeed = 20f;

    // Update is called once per frame
    void Update()
    {
        angle += rotationSpeed * Time.deltaTime;
        transform.localRotation = Quaternion.Euler(angle, 0.0f, 0.0f);
    }
}

Adjust the axis depending on your needs. By multiplying the rotation speed with Time.deltaTime, you get a steady rotation that happens at your specified speed.

You need to incrementally grow your angle. On the first frame it’s 0, on the second frame it’s incremented with rotationSpeed * Time.deltaTime… and so on.

It doesn’t affect the X-axis it only rotate on the Y-axis

angle += rotationSpeed * Time.deltaTime;
        Sky.transform.localRotation = Quaternion.Euler(0.0f, angle, -30f);

No, it definitely should rotate around the axis you set to rotate. But it’s hard for me to guess what else you have in your scene and so on.

That’s what i’m trying to do