Help with imporoved scriping code to move an GameObject with a key press.

I was hoping to get some advice with a bit scripting.

I have imported a quite complex 3D Modo Mesh that I want to explode to show the many different parts of the 3D object with a key press. So I have created a script that will move out part of the 3D object when I press “Q” to a target position and when it has got to the target position if I press “Q” again it will move back to the original position. But before I go on and start scripting more of the objects that I want to explode, would like a little advice on if there is a cleaner / better way of doing this.

using UnityEngine;
using System.Collections;

public class ExplodeControl : MonoBehaviour {

public float speed = 10;
public GameObject q_Object = null;
public Transform targetPosition;

Vector3 newPosition;
Vector3 zeroPosition;
string objectMoving = "Start";

// Use this for initialization
void Start () {

    zeroPosition = q_Object.transform.position;

}

// Update is called once per frame
void Update () {

    if (Input.GetKey (KeyCode.Q))
    {
        Debug.Log("Q KEY PRESSED");
        if (objectMoving == "Start")
        {
            objectMoving = "Moving to Target";
        }
        if (objectMoving == "Target")
        {
            objectMoving = "Moving to Start";
        }

    }
        
    moveObject();
}

void moveObject () {

    if (objectMoving == "Moving to Target")
    {
        q_Object.transform.position = Vector3.MoveTowards(q_Object.transform.position, targetPosition.position, Time.deltaTime * speed);
        Debug.Log("Moving to Target");
    }

    if (objectMoving == "Moving to Start")
    {
        q_Object.transform.position = Vector3.MoveTowards(q_Object.transform.position, zeroPosition, Time.deltaTime * speed);
        Debug.Log("Moving to Start");
    }

    if (Vector3.Distance(q_Object.transform.position, targetPosition.position) < .001f)
    {
        objectMoving = "Target";
        Debug.Log("I'm at " + objectMoving);
    }

    if (Vector3.Distance(q_Object.transform.position, zeroPosition) < .001f)
    {
        objectMoving = "Start";
        Debug.Log("I'm at " + objectMoving);
    }
}

}

The first thing I could recommend you is: Avoid string comparison. An enum seems to suit better your “state” coding intent. Example below:

 enum ObjectState {
     START,
     TARGET,
     MOVING_TO_TARGET,
     MOVING_TO_START,
 }
 
 // (...)
 
 ObjectState objectMoving = ObjectState.START;
 
 // (...)
 
 // You may use it like this...
 switch (objectMoving)
 {
     case ObjectState.START:
         objectMoving = ObjectState.MOVING_TO_TARGET;
         break;
 
     case ObjectState.TARGET:
         objectMoving = ObjectState.MOVING_TO_START;
         break;
 
     default:
       // If you need an <else> statement, you can put it here
       break;
 }
 
 // Or like this...
 switch (objectMoving)
 {
     case ObjectState.START:
         objectMoving = ObjectState.MOVING_TO_TARGET;
         break;

     // In this case, the default value is <ObjectState.TARGET>
     default:
     case ObjectState.TARGET:
         objectMoving = ObjectState.MOVING_TO_START;
         break;
 }

Also, according to the ARM Guide for Code Optimization in Unity, avoid making GetComponent calls. And you are doing it in Update function through moveObject method, because this.gameObject and this.transform are disguised GetComponent calls: q_Object.transform. ...

Consider caching these objects into variables:

GameObject _obj;
Transform _tr;

void Awake ()
{
    _obj = gameObject; //GetComponent<GameObject>();
    _tr = transform;   //GetComponent<Transform>();
}

// (...)

_tr.position = Vector3. (...)
_obj.SetActive(true);

Thanks for your help I will read more in to the CASE statements and implement this method.