Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Hedonsoft · Nov 08, 2015 at 01:23 AM · rotationeditorpositionvector3

How do I reset a GameObject pos / rot with a toggle?

I'm creating an Editor script and have a toggle field to that is supposed to go between the local position and rotation to the parent object, when the toggle if off I unparent the object and want it to return to the world origin.

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Weaponizer : EditorWindow {
     #region Public Variables
     public static Weaponizer curWindow;
     Color color = Color.red;
     public WeaponControl weaponControl;
     public GameObject weaponGO;
     public GameObject character;
     public GameObject weapon;
     public GameObject bulletSpawn;
     public GameObject bulletPrefab;
     public GameObject leftHandPosGO;
 
     public Transform rightHandTransform;
 
     public string weaponName = "";
     #endregion
 
     #region Private Variables
     bool charExists = false;
     bool weaponExists = false;
     bool localPos = false;
 
     Vector3 defaultWeaponPos;
     Quaternion defaultWeaponPRot;
     #endregion
 
     #region Main methods
     // Use this for initialization
     void OnEnable () {
         InitEditorWindow();
 
     }
 
     void OnGUI()
     {
 
         GUILayout.BeginHorizontal("box");
         GUILayout.BeginVertical();
         GUILayout.Label("Weaponizer");
         EditorGUIUtility.labelWidth = 70f;
 
         character = (GameObject)EditorGUILayout.ObjectField("Character", character, typeof(GameObject), false);
         if (character)
         {
             // If Character GameObject field is populated with character then draw Instantiation button
             if (GUILayout.Button("Add Character", GUILayout.Height(30)))
             {
                 Instantiate(character, Vector3.zero, Quaternion.identity);
                 character.name = "Character";
                 charExists = true;
 
                 rightHandTransform = GameObject.Find("RightHand").transform;
                 weaponGO = GameObject.Find("Weapon");
                 defaultWeaponPos = weaponGO.transform.position;
                 defaultWeaponPRot = weaponGO.transform.rotation;
                 weaponGO.transform.parent = rightHandTransform;
                 
                 charExists = true;
             }
 
         if (charExists==true) {
             weapon = (GameObject)EditorGUILayout.ObjectField("Weapon", weapon, typeof(GameObject), false);
             EditorGUIUtility.labelWidth = 110f;
             weaponName = EditorGUILayout.TextField("Weapon name", weaponName);
             if (weapon)
             {
                 weaponControl = weaponGO.GetComponent<WeaponControl>();
                 if (GUILayout.Button("Weapon", GUILayout.Height(30)))
                 {
                     weapon = (GameObject)Instantiate(weapon, Vector3.zero, Quaternion.identity);
                     if (weaponName != "")
                     {
                         weapon.name = weaponName;
                         if (rightHandTransform)
                         {
                             weapon.transform.parent = rightHandTransform;
                             weapon.transform.localPosition = Vector3.zero;
                             weapon.transform.localRotation = Quaternion.identity;
                                 weapon.transform.parent = weaponGO.transform;
                                 weaponExists = true;                            }
                     }
                     else
                     {
                         EditorUtility.DisplayDialog("Attention", "Please enter a name", "Cancel");
                     }
                     
                 }
             }
         }
 
         //If character is instantiated draw the rest of the GUI
             GUILayout.Label("Targets");
         if (weapon) {
             leftHandPosGO = (GameObject)EditorGUILayout.ObjectField("Left Hand Pos", leftHandPosGO, typeof(GameObject), false);
             if (GUILayout.Button ("Left Hand Target", GUILayout.Height (30))) {
                 GameObject leftHand = new GameObject ("Left Hand Target");
                 leftHandPosGO = leftHand;
                 leftHand.transform.parent = weaponGO.transform;
                 leftHand.transform.localPosition = Vector3.zero;
                 weaponControl.Children.Add (leftHand);
                 weaponControl.HandPosition = leftHand;
             }
         }
 
         if (weapon)
         {
 
              bulletSpawn = (GameObject)EditorGUILayout.ObjectField("Bullet Spawn", bulletSpawn, typeof(GameObject), false);
             if (GUILayout.Button("Bullet Spawn Point", GUILayout.Height(30)))
             {
                 bulletSpawn = new GameObject("Bullet Spawn");
                 bulletSpawn.transform.parent = weaponGO.transform;
                 bulletSpawn.transform.position = Vector3.zero;
                 weaponControl.Children.Add(bulletSpawn);
                 weaponControl.bulletSpawn = bulletSpawn.transform;
             }
 
             bulletPrefab = (GameObject)EditorGUILayout.ObjectField("Bullet Prefab", bulletPrefab, typeof(GameObject), false);
             if (GUILayout.Button("Bullet Prefab", GUILayout.Height(30)))
             {
                 bulletPrefab = (GameObject)Instantiate(bulletPrefab, Vector3.zero, Quaternion.identity);
                 bulletPrefab.transform.parent = weaponGO.transform;
                 bulletPrefab.transform.position = Vector3.zero;
                 weaponControl.Children.Add(bulletPrefab);
                 weaponControl.bulletPrefab = bulletPrefab;
             }
 
             localPos= GUILayout.Toggle(localPos, "local or Global Positioning");
             if (localPos)
             {
                     Debug.Log(defaultWeaponPos+" " + weaponGO.transform.position);
                     weaponGO.transform.parent = GameObject.Find("RightHand").transform;
                 }
             else
             { 
                     weaponGO.transform.parent = null;
                     weaponGO.transform.position = new Vector3(0, 0, 0);
                     weaponGO.transform.rotation = Quaternion.identity;
                     Debug.Log(defaultWeaponPos + " " + weaponGO.transform.position);
                 }
             }  
         }
 
         GUILayout.EndVertical();
         GUILayout.EndHorizontal();   
     }
 
     public static void InitEditorWindow()
     {
         curWindow = (Weaponizer)EditorWindow.GetWindow<Weaponizer>();
         curWindow.titleContent = new GUIContent("Weapon Editor");
     }
     #endregion
 
     #region Utility Methods
     #endregion
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

39 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

RTS Camera movement wrong after rotation 1 Answer

Quick Angles Question 2 Answers

Instantiating objects at position 1 Answer

How would I find a point in front of my cam? 2 Answers

Setting value of transform.rotation in code, C# 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges