Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. Setup:
  2.  
  3. - Empty scene with a camera and directional light
  4. - One game object, placed at world space origin
  5. - Should have some geometry (so it's visible)
  6. - This will be the parent of the AgentController script...
  7.  
  8. - One prefab:
  9. - Geometry so it's visible
  10. - Parent of the AgentBehaviour Script
  11. - Sphere collider so physics checks work
  12. - Added to a custom physics layer
  13.  
  14.  
  15. Agent Controller Script:
  16. ------------------------
  17.  
  18. - Should expose variables:
  19. - Number of agents to spawn at startup
  20. - Spawn Radius
  21. - Velocity agents should move at
  22. - Speed agents can rotate at
  23. - Distance agents should try to maintain from each other
  24. - LayerMask your Agent prefab is in (for the physics test)
  25. - Holder for the prefab reference
  26.  
  27. Start()
  28. - Spawn fSpawnCount agents
  29.  
  30. Spawn()
  31. - Instantiate the agent prefab
  32. - Give it _small_ amount of random rotation for variation
  33. - Find the prefab's AgentBehaviour script:
  34. - Set its reference to AgentController to <this>
  35. - ie: make sure the AgentBehaviour script knows where AgentController is!
  36.  
  37.  
  38.  
  39. Agent Behaviour Script
  40. ----------------------
  41.  
  42. - Should expose variables:
  43. - AgentController
  44.  
  45.  
  46. Vector3 GetSeparationVector(Transform target)
  47. {
  48. var diff = transform.position - target.transform.position;
  49. var diffLen = diff.magnitude;
  50. var scaler = Mathf.Clamp01(1.0f - diffLen / controller.neighborDist);
  51. return diff * (scaler / diffLen);
  52. }
  53.  
  54. Start()
  55. - Empty
  56.  
  57.  
  58. Update()
  59. - Store Current Position and Current rotation
  60. - Velocity = Controller.Velocity
  61. - Create 3 vectors:
  62. - separation = vector3.zero
  63. - alignment = controller's forward vector
  64. - cohesion = controller's Position
  65. - Look for agents:
  66. - Physics.OverlapSphere(currentPosition, Neighbour Distance, Physics Layer to Search);
  67. - For each agent:
  68. - separation += GetSeparationVector(agent.transform)
  69. - alignment += agent's forward vector
  70. - cohesion += agent's position
  71. - float average = 1.0f / number of agents found by the Physics.OverlapSphere check previously
  72. - alignment *= avgerage
  73. - cohesion *= average
  74. - cohesion = (cohesion - currentPosition).normalized;
  75. - new direction = separation + alignment + cohesion;
  76. - new rotation = Quaternion.FromToRotation(Vector3.forward, direction.normalized);
  77. - if (rotation != currentRotation) {
  78. transform.rotation = Quaternion.Slerp(rotation, currentRotation, Mathf.Exp(-4.0f * Time.deltaTime));
  79. }
  80. - new Agent position = currentPosition + transform forward * (Velocity * Time.deltaTime);
  81.  
  82.  
  83.  
  84. Hidden Coins
  85. ------------
  86.  
  87. - Add buttons to spawn new sets of agents
  88. - Move the gameobject the controller is on, so as to guide the agents around an area
  89. - Make the camera into an agent-flock-follow cam, targetting the centre of the flock
  90. - Post effects, blur, etc.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement