Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 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. - The position of which should be within "spawn radius"
  33. - Give it _small_ amount of random rotation for variation
  34. - Find the prefab's AgentBehaviour script:
  35. - Set its reference to AgentController to <this>
  36. - ie: make sure the AgentBehaviour script knows where AgentController is!
  37.  
  38.  
  39.  
  40. Agent Behaviour Script
  41. ----------------------
  42.  
  43. - Should expose variables:
  44. - AgentController
  45.  
  46.  
  47. Vector3 GetSeparationVector(Transform target)
  48. {
  49. var diff = transform.position - target.transform.position;
  50. var diffLen = diff.magnitude;
  51. var scaler = Mathf.Clamp01(1.0f - diffLen / controller.neighborDist);
  52. return diff * (scaler / diffLen);
  53. }
  54.  
  55. Start()
  56. - Empty
  57.  
  58.  
  59. Update()
  60. - Store Current Position and Current rotation
  61. - Velocity = Controller.Velocity
  62. - Create 3 vectors:
  63. - separation = vector3.zero
  64. - alignment = controller's forward vector
  65. - cohesion = controller's Position
  66. - Look for agents:
  67. - Physics.OverlapSphere(currentPosition, Neighbour Distance, Physics Layer to Search);
  68. - For each agent:
  69. - separation += GetSeparationVector(agent.transform)
  70. - alignment += agent's forward vector
  71. - cohesion += agent's position
  72. - float average = 1.0f / number of agents found by the Physics.OverlapSphere check previously
  73. - alignment *= avgerage
  74. - cohesion *= average
  75. - cohesion = (cohesion - currentPosition).normalized;
  76. - new direction = separation + alignment + cohesion;
  77. - new rotation = Quaternion.FromToRotation(Vector3.forward, direction.normalized);
  78. - if (rotation != currentRotation) {
  79. transform.rotation = Quaternion.Slerp(rotation, currentRotation, Mathf.Exp(-4.0f * Time.deltaTime));
  80. }
  81. - new Agent position = currentPosition + transform forward * (Velocity * Time.deltaTime);
  82.  
  83.  
  84.  
  85. Hidden Coins
  86. ------------
  87.  
  88. - Add buttons to spawn new sets of agents
  89. - Move the gameobject the controller is on, so as to guide the agents around an area
  90. - Make the camera into an agent-flock-follow cam, targetting the centre of the flock
  91. - Post effects, blur, etc.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement