Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Setup:
- - Empty scene with a camera and directional light
- - One game object, placed at world space origin
- - Should have some geometry (so it's visible)
- - This will be the parent of the AgentController script...
- - One prefab:
- - Geometry so it's visible
- - Parent of the AgentBehaviour Script
- - Sphere collider so physics checks work
- - Added to a custom physics layer
- Agent Controller Script:
- ------------------------
- - Should expose variables:
- - Number of agents to spawn at startup
- - Spawn Radius
- - Velocity agents should move at
- - Speed agents can rotate at
- - Distance agents should try to maintain from each other
- - LayerMask your Agent prefab is in (for the physics test)
- - Holder for the prefab reference
- Start()
- - Spawn fSpawnCount agents
- Spawn()
- - Instantiate the agent prefab
- - Give it _small_ amount of random rotation for variation
- - Find the prefab's AgentBehaviour script:
- - Set its reference to AgentController to <this>
- - ie: make sure the AgentBehaviour script knows where AgentController is!
- Agent Behaviour Script
- ----------------------
- - Should expose variables:
- - AgentController
- Vector3 GetSeparationVector(Transform target)
- {
- var diff = transform.position - target.transform.position;
- var diffLen = diff.magnitude;
- var scaler = Mathf.Clamp01(1.0f - diffLen / controller.neighborDist);
- return diff * (scaler / diffLen);
- }
- Start()
- - Empty
- Update()
- - Store Current Position and Current rotation
- - Velocity = Controller.Velocity
- - Create 3 vectors:
- - separation = vector3.zero
- - alignment = controller's forward vector
- - cohesion = controller's Position
- - Look for agents:
- - Physics.OverlapSphere(currentPosition, Neighbour Distance, Physics Layer to Search);
- - For each agent:
- - separation += GetSeparationVector(agent.transform)
- - alignment += agent's forward vector
- - cohesion += agent's position
- - float average = 1.0f / number of agents found by the Physics.OverlapSphere check previously
- - alignment *= avgerage
- - cohesion *= average
- - cohesion = (cohesion - currentPosition).normalized;
- - new direction = separation + alignment + cohesion;
- - new rotation = Quaternion.FromToRotation(Vector3.forward, direction.normalized);
- - if (rotation != currentRotation) {
- transform.rotation = Quaternion.Slerp(rotation, currentRotation, Mathf.Exp(-4.0f * Time.deltaTime));
- }
- - new Agent position = currentPosition + transform forward * (Velocity * Time.deltaTime);
- Hidden Coins
- ------------
- - Add buttons to spawn new sets of agents
- - Move the gameobject the controller is on, so as to guide the agents around an area
- - Make the camera into an agent-flock-follow cam, targetting the centre of the flock
- - Post effects, blur, etc.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement