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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Making an object rotate around itself

Discussion in 'Scripting' started by Larpushka, Aug 24, 2015.

Thread Status:
Not open for further replies.
  1. Larpushka

    Larpushka

    Joined:
    Jan 6, 2015
    Posts:
    214
    When I rotate an object using the rotation icon in the editor it rotates around itself, but when I try to do it with a script it appears to be rotating around a different axis (not necessarily around itself).. How can I fix it?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SimpleRotate : MonoBehaviour {
    5.     void Update () {
    6.         transform.Rotate (Vector3.right * 50 * Time.deltaTime, Space.World);
    7.  
    8.     }
    9. }
    10.  
     
  2. lineupthesky

    lineupthesky

    Joined:
    Jan 31, 2015
    Posts:
    92
    Have you tried rotating it in the Up axis?

    Code (CSharp):
    1.  transform.Rotate (Vector3.up * 50 * Time.deltaTime, Space.Self);
     
  3. Larpushka

    Larpushka

    Joined:
    Jan 6, 2015
    Posts:
    214
    That just changes the direction of rotation, but not the axis of rotation.
     
  4. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
  5. Larpushka

    Larpushka

    Joined:
    Jan 6, 2015
    Posts:
    214
    Changing Space.World to Space.Self didn't make a different
     
  6. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    there only 3, with values are wrong ? how should it rotate how is it rotating, look in your inspector for the values.

    I just tried out a little script, a cylinder rotating itself around his middle point in y axis.

    Code (CSharp):
    1. public class Rotate : MonoBehaviour {
    2.  
    3.     void Update()
    4.     {
    5.         transform.Rotate( new Vector3(0, 0.3f, 0) );
    6.     }
    7. }
     
    strikerplayz likes this.
  7. Larpushka

    Larpushka

    Joined:
    Jan 6, 2015
    Posts:
    214
    Guys, I think you all misunderstood me, and I finally understood the core of my problem. Since my objects' points of rotation are not in the middle, unless I do a funny workaround it's best just to re-export it in a 3D modeling program with the axis in the middle. That ought to solve my problem.
     
  8. zelsas

    zelsas

    Joined:
    Jun 21, 2019
    Posts:
    2
    Hey guys. Does anyone knows how to rotate an object just once? Cause i have not idea and im desperate.
     
  9. SULABEEE

    SULABEEE

    Joined:
    Dec 17, 2017
    Posts:
    2
    An easier solution would be creating an empty object and place the object that you want to rotate inside it
     
    rivstyx likes this.
  10. rivstyx

    rivstyx

    Joined:
    Sep 6, 2018
    Posts:
    38
    Yes. This is exactly how I do it.
     
    silohunter likes this.
  11. defunct_artist

    defunct_artist

    Joined:
    Jun 6, 2019
    Posts:
    3
    TLDR: If you have a 3d model on a parent empty game object, make sure the child's position is 0, 0, 0.

    After having this same problem, I figured it out.

    My original "player" game object, consisting of a 3d model childed to an empty game object and with similar code to the above, was rotating around a random point for no particular reason
    I deleted and rebuilt my player game object with an empty and a simple cube shape. I have the setting to place objects at the world origin when I create them in the hierarchy, so both the empty and 3d object's positions were the same.

    When I reimported my player graphic and parented it to the empty, I noticed it (the child's) position was not Zero'd, however that was not obvious when just clicking the empty parent game object and viewing it in the inspector.

    Thus, make sure the child game object's position is 0, 0, 0.
     
    Cinged likes this.
  12. silohunter

    silohunter

    Joined:
    May 20, 2022
    Posts:
    1
    Best answer for this .... thanks mate !!
     
  13. Cinged

    Cinged

    Joined:
    Aug 11, 2022
    Posts:
    2

    This is exactly what I came to figure out. Once I zero’d all the children’s position coordinates, placed the parent object, moved the children in relation to the parent, then it spun around a center anchor point. Thank you so much!
     
  14. JM4r10

    JM4r10

    Joined:
    Mar 11, 2023
    Posts:
    1
    If you add a BoxCollider to the empty object it would tell you exactly where is its pivot, this really helped me to visualize where to put the children objects in relation to their parent.
     
  15. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,560
    That's bad idea considering that's an active component that has behaviour and is part of a build i.e. a hammer to scratch and itch.

    Just use the built-in custom icons in the Inspector window (See: "Assign custom icons to GameObjects, scripts, and Prefabs"). Adding in a small circular coloured dot should suffice?

    Note: Please bear in mind that this thread is very, very old so please try to avoid necroing posts.

    Thanks.
     
Thread Status:
Not open for further replies.