Talk:CoUpdate

From Unify Community Wiki
Revision as of 00:02, 23 August 2011 by BDev (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

There's alot of problems with the example code, and the idea of starting 2 coroutines is kind of overkill heres your example but utilizing built in features of unity.

<javascript>// CoUpdate usage example. // Assumes an Animation is used.

  1. pragma strict

@script RequireComponent(Animation)

// Changing start to return IEnumerator allows you to do yield statements. function Start() : IEnumerator {

   // do start work here.
   // this is the update loop. called untill the object is destroyed.
   do { 
       yield return StartCoroutine( CoUpdate() );
       yield return WaitForEndOfFrame();// let the frame advance.
   } while(true);

}

/* Not needed function CoStart() : IEnumerator {

   while (true)
       yield CoUpdate();

}*/

function CoUpdate() : IEnumerator {

   // Notice how easily we can taunt 
   // and render movement disabled
   // for the duration since Taunt is
   // a Coroutine.
   
   if (Input.GetKey(KeyCode.G))
       yield StartCoroutine( Taunt() );// this should use StartCoroutine unless Taunt returned a YieldStatement which it really should/could for what its doing now. 
   Move();

}

function Taunt() : IEnumerator {

   animation.Play(animation.clip.name);
   yield WaitForSeconds(animation.clip.length);

}

function Move() {

   if (Input.GetKey(KeyCode.LeftArrow))
       Move(Vector3.left);
   if (Input.GetKey(KeyCode.RightArrow))
       Move(Vector3.right);
   if (Input.GetKey(KeyCode.UpArrow))
       Move(Vector3.forward);
   if (Input.GetKey(KeyCode.DownArrow))
       Move(Vector3.back);

}

function Move(direction : Vector3) {

   transform.Translate(direction * Time.deltaTime, Space.Self);

}</javascript>


Really though, the whole idea of CoUpdate can be accomplished by using Start(): IEnumerator and in the never ending loop just yield WaitForEndOfFrame.

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox