20150220

Unity3D - Swirl Demo

I'm working on a demo video, where I want to show how the lack of co-routine makes the web freeze up the game.  To do that, I just needed something smoothly moving around.

The code gives room for variation, to allow different angles, and the cubes use trail renderer.
using UnityEngine;
using System.Collections;

public class Orbit : MonoBehaviour {

 public GameObject Target;
 public Vector3 direction = Vector3.left;
 public float Speed = 0.1f;
 public bool Randomize = false;
 public float RandomizeMin = -0.5f;
 public float RandomizeMax = 0.5f;
 public float MaxDistance = 5;
 public float MinDistance = 1;
 
 void Update () {
  this.transform.LookAt(Target.transform);
  var shift = Vector3.zero;
  if (Randomize)
  {
   shift = new Vector3(Random.Range (RandomizeMin, RandomizeMax),
        Random.Range (RandomizeMin, RandomizeMax) * Time.deltaTime,
        Random.Range (RandomizeMin, RandomizeMax));
  }
  this.transform.Translate ((shift + direction) * Speed * Time.deltaTime);
  if (Vector3.Distance(Target.transform.position, transform.position) > MaxDistance)
  {
   Debug.Log ("To Far");
   this.transform.position = this.Target.transform.position + Vector3.ClampMagnitude(
    this.transform.position - this.Target.transform.position,
    this.MaxDistance);
  }
  if (Vector3.Distance(Target.transform.position, transform.position) < MinDistance)
  {
   Debug.Log ("To Close");
   this.transform.position = this.Target.transform.position + Vector3.ClampMagnitude(
    (this.transform.position - this.Target.transform.position) * 100,
    this.MinDistance);
  }
 }
}

No comments: