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:
Post a Comment