- Home /
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
Your answer
Follow this Question
Related Questions
RTS Camera movement wrong after rotation 1 Answer
Quick Angles Question 2 Answers
Instantiating objects at position 1 Answer