ToggleGravity

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
Line 1: Line 1:
 +
[[Category: Concepts]]
 +
[[Category: MonoBehaviour]]
 +
[[Category: JavaScript]]
 +
[[Category: Mouse]]
 +
[[Category: Gravity]]
 
Author: Jonathan Czeck (aarku)
 
Author: Jonathan Czeck (aarku)
 
== Description ==
 
== Description ==

Revision as of 12:14, 22 December 2005

Author: Jonathan Czeck (aarku)

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.

Usage

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

JavaScript - ToggleGravity.js

<javascript> private var gravitySave : Vector3; private var gravityOn = true;

function Update() {

   if (Input.GetMouseButtonDown(0))
   {
       if (gravityOn)
       {
           gravitySave = Physics.gravity;
           Physics.gravity = Vector3.zero;
           gravityOn = false;
       }
       else
       {
           Physics.gravity = gravitySave;
           gravityOn = true;
       }
   }

} </javascript>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox