Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Hotkey to reset transform on game object?

Discussion in 'Getting Started' started by Zafaron, Dec 21, 2018.

  1. Zafaron

    Zafaron

    Joined:
    Dec 20, 2018
    Posts:
    7
    I've been going through some of the tutorials and I noticed that I have to reset the transform on an object very often. I was just wondering what the hotkey was for that as I have been unable to locate it myself. Thanks.
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    5,403
    at least you could create editor script for it, something like

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. // reset transform position, rotation and scale
    5.  
    6. namespace UnityLibrary
    7. {
    8.     public class ResetTransform : ScriptableObject
    9.     {
    10.         [MenuItem("GameObject/Reset Transform #r")]
    11.         static public void MoveSceneViewCamera()
    12.         {
    13.            // TODO: add multiple object support
    14.             var go = Selection.activeGameObject;
    15.             if (go != null)
    16.             {
    17.                 // TODO: add undo
    18.  
    19.                 go.transform.position = Vector3.zero;
    20.                 go.transform.rotation = Quaternion.identity;
    21.                 go.transform.localScale = Vector3.one;
    22.             }
    23.         }
    24.  
    25.     } // class
    26. } // namespace
    27.  
    * https://github.com/UnityCommunity/U...s/Scripts/Editor/GameObject/ResetTransform.cs

    https://docs.unity3d.com/ScriptReference/MenuItem.html
     
  3. Zafaron

    Zafaron

    Joined:
    Dec 20, 2018
    Posts:
    7
    Thanks for this, it looks to do exactly what I wanted to. If I read the documentation right, #r is shift-r? Also, do these scripts that add functionality to the editor itself go into an editor folder in each project? Or is there a place to add them so they only need to be added one time and will work on multiple projects?
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    5,403
    yes, these editor scripts need to be in editor/ folder (within the project), not necessary in the root.
    so its fine to have
    assets/yourfolder/somethingelse/editor
    assets/editor/myscripts/here
    etc..

    correct its shift-r, you can change that to something else of course.
    (i guess even single letter keys work, unless used by unity)
     
  5. Navarth

    Navarth

    Joined:
    Dec 17, 2017
    Posts:
    5
    This is awesome, but is there a way to do this automatically on creating an empty gameobject?

    I understand that most objects are things worth placing in sight of the viewport, as they'll need to be moved in 3D space either way, but empty game objects almost always require a manual transform reset, and it would be really nice to zero them out on creation with an automated script.
     
  6. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    729
    Yep, I hate-hate-hate how transforms are almost always jacked up when I drop things in the hierarchy or create objects.

    Make a hotkey that creates an empty as a child of the current selected object, with its transform reset. :) And then another, creating an Empty at the current selected object's location and orientation, and the current object made a child with its own transform reset... Extending Unity with your own Editor tweaks is addictive.
     
  7. Navarth

    Navarth

    Joined:
    Dec 17, 2017
    Posts:
    5
    Is there an event that gets called on creating an empty game object I could hook into? Frankly my ideal solution wouldn't even be a hotkey - I want reset transforms on empties to be the default behaviour.

    In the remote chance I ever need one somewhere else, I think I'll survive the trip to the origin to go find it!
     
  8. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    6,495
    Is this happening when creating new GameObjects in the root of the scene? I thought I only saw this when I create them as a child of another GameObject. Maybe I'm remembering wrong.
     
  9. Navarth

    Navarth

    Joined:
    Dec 17, 2017
    Posts:
    5
    It does indeed. Just creating an empty game object in the scene panel or from the Game Object menu bar results in a new empty game object with a seemingly random transform, meaning each time I create one I need to manually reset it before I group anything under it..
     
  10. Dan-Smart

    Dan-Smart

    Joined:
    Sep 2, 2012
    Posts:
    5
    I added the support for undo, multiple selected objects, and also made it so if the objects are parented that the parent offset is applied to the child. Pretty useful script, it must be said!



    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. public class ResetGameObjectPosition
    5. {
    6.     [MenuItem("GameObject/Reset Transform #r")]
    7.     static public void MoveSceneViewCamera()
    8.     {
    9.         GameObject [] selectedObjects = Selection.gameObjects;
    10.        
    11.         foreach( GameObject selected_object in selectedObjects )
    12.         {
    13.             Undo.RegisterCompleteObjectUndo(selected_object.transform, "Reset game object to origin");
    14.  
    15.             Vector3 p_pos = Vector3.zero;
    16.             Quaternion p_rot = Quaternion.identity;
    17.             Vector3 p_scale = Vector3.one;
    18.  
    19.             if( selected_object.transform.parent != null )
    20.             {
    21.                 p_pos = selected_object.transform.parent.position;
    22.                 p_rot = selected_object.transform.parent.rotation;
    23.                 p_scale = selected_object.transform.parent.localScale;
    24.             }
    25.  
    26.             selected_object.transform.position = Vector3.zero + p_pos;
    27.             selected_object.transform.rotation = Quaternion.identity * p_rot;
    28.             selected_object.transform.localScale = Vector3.one;
    29.         }
    30.     }
    31. }
     
    Comafly likes this.