Beginner's Scripting Guide

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Fixed language tags)
m (Fixed wacky indentation and made brace usage consistent)
Line 9: Line 9:
 
<javascript>function Update()  
 
<javascript>function Update()  
 
{
 
{
for (var i=0; i < someNumber; i++) {
+
for (var i=0; i < someNumber; i++)
    something something;
+
{
        }
+
something something;
 +
}
 
}</javascript>
 
}</javascript>
  
Line 17: Line 18:
 
<javascript>function ToggleRenderers()  
 
<javascript>function ToggleRenderers()  
 
{
 
{
        var aList = GetComponentsInChildren(Renderer);
+
var aList = GetComponentsInChildren(Renderer);
for ( var r in aList ) {
+
for ( var r in aList )
    r.enabled= ! r.enabled;
+
{
        }
+
r.enabled= ! r.enabled;
 +
}
 
}</javascript>
 
}</javascript>
  
Line 29: Line 31:
  
 
if (target)
 
if (target)
  StartCoroutine("Lerp", 1.0);
+
{
 +
StartCoroutine("Lerp", 1.0);
 +
}
  
 
function Lerp (time : float)
 
function Lerp (time : float)
 
{
 
{
  var pos = transform.position;
+
var pos = transform.position;
  var rot = transform.rotation;
+
var rot = transform.rotation;
  var originalTime = time;
+
var originalTime = time;
+
 
  while (time > 0.0)
+
while (time > 0.0)
  {
+
{
      time -= Time.deltaTime;
+
time -= Time.deltaTime;
      transform.position = Vector3.Lerp(target.position, pos, time / originalTime);
+
transform.position = Vector3.Lerp(target.position, pos, time / originalTime);
      transform.rotation = Quaternion.Slerp(target.rotation, rot, time / originalTime);
+
transform.rotation = Quaternion.Slerp(target.rotation, rot, time / originalTime);
      yield;
+
yield;
  }
+
}
 
}</javascript>
 
}</javascript>

Revision as of 13:59, 8 January 2011

This article is a stub.

You can help UnifyWiki by expanding it.


Help, I need a Loop Guru!

If you find you want to loop (i.e. for your co-routines) but are not sure about what type of loop, you've come to the right place! Here are a list of useful example loops;

The for loop

Basic "for" loop; start value, end value, update expression (operator i increases on each update): <javascript>function Update() { for (var i=0; i < someNumber; i++) { something something; } }</javascript>

The for loop can also be used to iterate of the contents of collections (ie. lists and arrays) <javascript>function ToggleRenderers() { var aList = GetComponentsInChildren(Renderer); for ( var r in aList ) { r.enabled= ! r.enabled; } }</javascript>

The while loop

A 'while' loop in a co-routine: <javascript>var target : Transform;

if (target) { StartCoroutine("Lerp", 1.0); }

function Lerp (time : float) { var pos = transform.position; var rot = transform.rotation; var originalTime = time;

while (time > 0.0) { time -= Time.deltaTime; transform.position = Vector3.Lerp(target.position, pos, time / originalTime); transform.rotation = Quaternion.Slerp(target.rotation, rot, time / originalTime); yield; } }</javascript>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox