Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Local rotation

Discussion in 'Scripting' started by Tornado77, Oct 27, 2019.

  1. Tornado77

    Tornado77

    Joined:
    Nov 14, 2018
    Posts:
    83
    Hi, how could I rotate an object around the local Y-axis? (Something like this)
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    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.
     
  3. Tornado77

    Tornado77

    Joined:
    Nov 14, 2018
    Posts:
    83
    I tried to use Transform.localRotation but I don't understand how it works, usually I use transform.Rotate()
     
  4. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    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:

    Code (CSharp):
    1. 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.
     
  5. Tornado77

    Tornado77

    Joined:
    Nov 14, 2018
    Posts:
    83
    Maybe an animation would be more simple?
     
  6. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Well. It depends on what you are trying to achieve. That code can't get much simpler.
     
  7. Tornado77

    Tornado77

    Joined:
    Nov 14, 2018
    Posts:
    83
    I need to rotate the object every frame
     
  8. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Well then just do it in Update incrementally or whatever method you run to do your update.
     
  9. Tornado77

    Tornado77

    Joined:
    Nov 14, 2018
    Posts:
    83
    This is on update

     
  10. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    @Tornado77 try something simple like this:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RotateLocal : MonoBehaviour
    6. {
    7.     public float angle = 0f;
    8.     public float rotationSpeed = 20f;
    9.  
    10.     // Update is called once per frame
    11.     void Update()
    12.     {
    13.         angle += rotationSpeed * Time.deltaTime;
    14.         transform.localRotation = Quaternion.Euler(angle, 0.0f, 0.0f);
    15.     }
    16. }
    17.  
    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.
     
  11. Tornado77

    Tornado77

    Joined:
    Nov 14, 2018
    Posts:
    83
    It doesn't affect the X-axis it only rotate on the Y-axis

    Code (CSharp):
    1. angle += rotationSpeed * Time.deltaTime;
    2.         Sky.transform.localRotation = Quaternion.Euler(0.0f, angle, -30f);
     
  12. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    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.
     
  13. Tornado77

    Tornado77

    Joined:
    Nov 14, 2018
    Posts:
    83


    That's what i'm trying to do