can/should I use a Toggle button for constant camera movement across an Axis?

I am a digital artist by trade, and trying to hammer my way through scripting. I am hoping I am going about this the right way (I am starting by trying to build tiny scripts and build up on them.)

I am trying to make an on screen toggle switch, that when in the "on " state, starts the camera scrolling at a certain speed in a single axis, and when in the "off" stage stays where it stops.

I am using the built in Unity toggle that address 2 different scripts with an If..ELse statement, that addresses 2 scripts. My questions are: 1. How can I make the camera keep moving at a certain speed until the toggle is tripped again? Right now it only moves a set distance per click.

  1. Is the toggle button the appropriate GUI element for this or should I take a regular button and use some sort of detection to see if it has been clicked again? Can I embed the update function into another function?

  2. Bonus question, how to I change the image for the other state? Do I have to make a GUI style or skin or can I just tell it directly to use a different image?

I've been doing a lot of digging around but I haven't been able to get it right looking at the reference and other examples so far, but I am hoping to prevail out of sheer stubbornness.

This is what I have so far.. baby steps right?

var Ignition = false;
var engineOnText : Texture;
var engineOffText : Texture;
var CameraSpeed : float;

function EngineOn () {

    GetComponent("MainCamera"); transform.Translate(CameraSpeed * Time.deltaTime, 0, 0 );
    Debug.Log("on");

}

function EngineOff () {

         Debug.Log("Off");
}

function OnGUI () {

    var makeNewButton = GUI.Toggle (Rect (800, 800, 200, 200),  Ignition, engineOffText);

    // Check if the toggle was toggled
    if (makeNewButton != Ignition) {
        if (makeNewButton == true)
            EngineOff();
        else
        EngineOn();

        Ignition = makeNewButton;
    }
}

Hers's how you'd go about it at the very basic level (ignoring custom textures):

var ignition = false;
var speed : float;

function Update() {
    if (ignition) {
        transform.Translate( speed * Time.deltaTime, 0, 0 );    
    }   
}

function OnGUI () {    
    ignition = GUI.Toggle(toggleRect , ignition, "Ignition");
}

Note how the *GUI.Toggle function accepts the "ignition" value as one of its inputs (which determines the current state to display), and it also returns a value which is put back into the "ignition" variable. The value it returns is usually unchanged, except on frames where the toggle was actually clicked, when it returns the new toggled value.

To add graphics for the toggle states, you need to use a custom GUI Style for that toggle. This can be done fairly easily by adding a GUIStyle variable, and then dragging textures into the appropriate slots in the inspector. Your modified script would look like this:

var ignition = false;
var speed : float;
var style : GUIStyle;

function Update() {
    if (ignition) {
        transform.Translate( speed * Time.deltaTime, 0, 0 );    
    }   
}

function OnGUI () {    
    ignition = GUI.Toggle(toggleRect , ignition, "", style);
}

And then you would look in the inspector and expand the "style" variable shown in your script. Within, you will see the names of lots of states such as "Normal", "Hover", "Focused", etc. If you expand these, you'll see each has a background texture slot, and a text color selector. You can drag a texture into the background texture slot for each style to assign a texture to that state.

The states you need to modify for a toggle button are:

  • Normal
  • Active
  • On Normal
  • On Active

Hope this helps!

Well I hope I can help out , even though I don't ever use JS, I do use C#. And adding Ducks sample you can do pretty much what you asked.

What I cna try to do to help is show you a bit of a C# design I wrote for this Q&A session. Hopefully it can help you get you on your way if you don't want to stick to JS or just want some design experience.

This design tries to keep an open mind so you can add and remove components or states without to much trouble, which is a "hard-mode" for programming but in the long run is a big pay. I think there should be something in here that can help you get on your way. Feel free to ask more!

Good luck with the project, nice to see artists going coding, just don't overdo it ok , we still want our jobs :D

Here's the code:

    //A cheap way of getting the console to just post the log once.
    private bool postScreenOnce = false;    

    //An enumerator (think of it as a bunch of options and one of them can be true
    //You can be either on OR off, not both. Enum is better for bigger than 2 options
    //Just wanted to demonstrate a usefull C# function
    public enum ScreenToggleState
    {
        ScreenIsToggledOn,
        ScreenIsToggledOff,
        ScreenIsDestroyed
    }

    //Storing the state in a variable here
    public ScreenToggleState CurrentScreenState;    

    //Keeps everything updated for every frame
    void Update()
    {
        if(CurrentScreenState.ScreenIsDestroyed)
        {
            return;
        }

        UpdateScreenState();
        UpdateCameraPosition(CurrentScreenState);

        if (!postScreenOnce)
        {
            PrintState();
            postScreenOnce = true;
        }
    }

//When the screen changes state update the variable holding the state
public void UpdateScreenState
{
        //If [Condition1]
        //{ Change CurrentScreenState }     

        //If [Condition2]
        //{ Change CurrentScreenState }

        //...
        //...

        //If (LightningStrikesMyHouse)
        //{
        //    CurrentScreenState = ScreenToggleState.ScreenIsDestroyed;
        //}
}   

    //Receives the state so you can differentiate what to do in which situation
    //I am currently using the property via the class itself, just fill it in on
    //the THIS CLASS spot
    private void UpdateCameraPosition(ScreenToggleState myState)
    {
        switch(myState) 
        {
            case THIS CLASS.ScreenToggleState.ScreenIsToggledOn :
            //Make your own Vector3 and pass the variable here
            Camera.main.transform.Translate(cameraMovementVector);
                break;
            case THIS CLASS.ScreenToggleState.ScreenIsToggledOff :
            Camera.main.transform.Translate(cameraRight);
                break;
        }
    }

    //Your JS Equivalent for the 2 functions reporting to console
    public void PrintScreenState()
    {
        Debug.Log("The current screen state = " + CurrentScreenState);  
    }

Ah! Thank you! for both the scripts and the explanations, I see I was attempting a more complicated route. The JS is exactly what I was looking for, but the C is also great for me to look through and learn a little more about.