Advertisement
Guest User

Untitled

a guest
Mar 29th, 2011
179
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Player.js
  2.  
  3. var target : GameObject;
  4.  
  5. function Update() {
  6. if (Input.GetKeyDown(KeyCode.Tab))
  7. SendMessage("TargetNext");
  8. }
  9.  
  10. function LateUpdate() {
  11. if (target)
  12. Debug.DrawLine(transform.position, target.transform.position);
  13. }
  14.  
  15. function SetTarget(newTarget : GameObject) {
  16. target = newTarget;
  17. }
  18.  
  19. // Targetter.js
  20.  
  21. var target : GameObject;
  22. var autoTarget : boolean = true;
  23.  
  24. if (autoTarget) {
  25. TargetRandom();
  26. }
  27.  
  28. function Update() {
  29. if (autoTarget && !(target /* && target.active */))
  30. TargetRandom();
  31. }
  32.  
  33. function TargetRandom() {
  34. var targets = FindTargets();
  35. if (targets.Count > 0) {
  36. var next = Random.Range(0, targets.Count);
  37. SendMessage("SetTarget", targets[next], SendMessageOptions.DontRequireReceiver);
  38. }
  39. }
  40.  
  41. function TargetNext() {
  42. var targets = FindTargets();
  43. if (targets.Count > 0) {
  44. targets.Sort(ByClosest);
  45. var next = (targets.IndexOf(target) + 1) % targets.Count;
  46. SendMessage("SetTarget", targets[next], SendMessageOptions.DontRequireReceiver);
  47. }
  48. }
  49.  
  50. function FindTargets() {
  51. var targets = System.Collections.Generic.List.<GameObject>(4);
  52. targets.AddRange(GameObject.FindGameObjectsWithTag("Enemy"));
  53. targets.AddRange(GameObject.FindGameObjectsWithTag("Player"));
  54. targets.Remove(gameObject);
  55. return targets;
  56. }
  57.  
  58. function ByClosest(a : GameObject, b : GameObject) : int {
  59. var distA = Vector3.Distance(transform.position, a.transform.position);
  60. var distB = Vector3.Distance(transform.position, b.transform.position);
  61. return distA.CompareTo(distB);
  62. }
  63.  
  64. function SetTarget(newTarget : GameObject) {
  65. target = newTarget;
  66. }
Advertisement
RAW Paste Data Copied
Advertisement