From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Text replace - "<javascript>" to "<syntaxhighlight lang="javascript">")
m (Text replace - "</javascript>" to "</syntaxhighlight>")
Line 34: Line 34:

Latest revision as of 20:53, 10 January 2012

Author: Jonathan Czeck (aarku)

[edit] Description

This script illustrates how to toggle some property by clicking the mouse button. The script waits for the left mouse button to be pressed down. When this occurs, two courses of action can be taken dependant on the state of the gravityOn variable. If the gravity is currently on, save the gravity value in a variable, set the global gravity to zero, and change the state of gravityOn to false. If the gravity is currently off, restore the global gravity setting and set the state of the gravityOn variable to true.

[edit] Usage

Place this script on any GameObject in your scene. Logically it should go on a master control GameObject named something like "GameController."

[edit] JavaScript - ToggleGravity.js

private var gravitySave : Vector3;
private var gravityOn = true;
function Update()
    if (Input.GetMouseButtonDown(0))
        if (gravityOn)
            gravitySave = Physics.gravity;
            Physics.gravity =;
            gravityOn = false;
            Physics.gravity = gravitySave;
            gravityOn = true;
Personal tools