boids = Physics.OverlapSphere(transform.position, cohesionRadius);
Debug.DrawLine(transform.position, cohesion, Color.magenta);
using UnityEngine; public class Boid : MonoBehaviour { private Vector3 cohesion; private float cohesionRadius = 10; private Collider[] boids; void Update() { cohesion = Vector3.zero; boids = Physics.OverlapSphere(transform.position, cohesionRadius); foreach (var boid in boids) { cohesion += boid.transform.position; } cohesion = cohesion / boids.Length; Debug.DrawLine(transform.position, cohesion, Color.magenta); } }
InvokeRepeating("CalculateVelocity", 0, 1);
transform.position += velocity * Time.deltaTime;
using UnityEngine; public class Boid : MonoBehaviour { public Vector3 velocity; private float cohesionRadius = 10; private Collider[] boids; private Vector3 cohesion; private void Start() { InvokeRepeating("CalculateVelocity", 0, 1); } void CalculateVelocity() { velocity = Vector3.zero; cohesion = Vector3.zero; boids = Physics.OverlapSphere(transform.position, cohesionRadius); foreach (var boid in boids) { cohesion += boid.transform.position; } cohesion = cohesion / boids.Length; cohesion = cohesion - transform.position; velocity += cohesion; } void Update() { transform.position += velocity * Time.deltaTime; Debug.DrawRay(transform.position, cohesion, Color.magenta); } }
separation += (transform.position - boid.transform.position) / (transform.position - boid.transform.position).magnitude;
if ((transform.position - boid.transform.position).magnitude < separationDistance)
if (boid != collider && (transform.position - boid.transform.position).magnitude < separationDistance)
using UnityEngine; public class Boid : MonoBehaviour { public Vector3 velocity; private float cohesionRadius = 10; private float separationDistance = 5; private Collider[] boids; private Vector3 cohesion; private Vector3 separation; private int separationCount; private void Start() { InvokeRepeating("CalculateVelocity", 0, 1); } void CalculateVelocity() { velocity = Vector3.zero; cohesion = Vector3.zero; separation = Vector3.zero; separationCount = 0; boids = Physics.OverlapSphere(transform.position, cohesionRadius); foreach (var boid in boids) { cohesion += boid.transform.position; if (boid != collider && (transform.position - boid.transform.position).magnitude < separationDistance) { separation += (transform.position - boid.transform.position) / (transform.position - boid.transform.position).magnitude; separationCount++; } } cohesion = cohesion / boids.Length; cohesion = cohesion - transform.position; if (separationCount > 0) { separation = separation / separationCount; } velocity += cohesion + separation; } void Update() { transform.position += velocity * Time.deltaTime; Debug.DrawRay(transform.position, separation, Color.green); Debug.DrawRay(transform.position, cohesion, Color.magenta); } }
alignment += boid.GetComponent<Boid>().velocity;
using UnityEngine; public class Boid : MonoBehaviour { public Vector3 velocity; private float cohesionRadius = 10; private float separationDistance = 5; private Collider[] boids; private Vector3 cohesion; private Vector3 separation; private int separationCount; private Vector3 alignment; private void Start() { InvokeRepeating("CalculateVelocity", 0, 1); } void CalculateVelocity() { velocity = Vector3.zero; cohesion = Vector3.zero; separation = Vector3.zero; separationCount = 0; alignment = Vector3.zero; boids = Physics.OverlapSphere(transform.position, cohesionRadius); foreach (var boid in boids) { cohesion += boid.transform.position; alignment += boid.GetComponent<Boid>().velocity; if (boid != collider && (transform.position - boid.transform.position).magnitude < separationDistance) { separation += (transform.position - boid.transform.position) / (transform.position - boid.transform.position).magnitude; separationCount++; } } cohesion = cohesion / boids.Length; cohesion = cohesion - transform.position; if (separationCount > 0) { separation = separation / separationCount; } alignment = alignment / boids.Length; velocity += cohesion + separation + alignment; } void Update() { transform.position += velocity * Time.deltaTime; Debug.DrawRay(transform.position, separation, Color.green); Debug.DrawRay(transform.position, cohesion, Color.magenta); Debug.DrawRay(transform.position, alignment, Color.blue); } }
velocity += cohesion + separation + alignment*2;
velocity = Vector3.ClampMagnitude(velocity, maxSpeed);
using UnityEngine; public class Boid : MonoBehaviour { public Vector3 velocity; private float cohesionRadius = 10; private float separationDistance = 5; private Collider[] boids; private Vector3 cohesion; private Vector3 separation; private int separationCount; private Vector3 alignment; private float maxSpeed = 15; private void Start() { InvokeRepeating("CalculateVelocity", 0, 1f); } void CalculateVelocity() { velocity = Vector3.zero; cohesion = Vector3.zero; separation = Vector3.zero; separationCount = 0; alignment = Vector3.zero; boids = Physics.OverlapSphere(transform.position, cohesionRadius); foreach (var boid in boids) { cohesion += boid.transform.position; alignment += boid.GetComponent<Boid>().velocity; if (boid != collider && (transform.position - boid.transform.position).magnitude < separationDistance) { separation += (transform.position - boid.transform.position) / (transform.position - boid.transform.position).magnitude; separationCount++; } } cohesion = cohesion / boids.Length; cohesion = cohesion - transform.position; cohesion = Vector3.ClampMagnitude(cohesion, maxSpeed); if (separationCount > 0) { separation = separation / separationCount; separation = Vector3.ClampMagnitude(separation, maxSpeed); } alignment = alignment / boids.Length; alignment = Vector3.ClampMagnitude(alignment, maxSpeed); velocity += cohesion + separation * 10 + alignment * 1.5f; velocity = Vector3.ClampMagnitude(velocity, maxSpeed); } void Update() { if (transform.position.magnitude > 25) { velocity += -transform.position.normalized; } transform.position += velocity * Time.deltaTime; Debug.DrawRay(transform.position, separation, Color.green); Debug.DrawRay(transform.position, cohesion, Color.magenta); Debug.DrawRay(transform.position, alignment, Color.blue); } }
Instantiate(boidPrefab, Random.insideUnitSphere * 25, Quaternion.identity);
using UnityEngine; public class HeartOfTheSwarm : MonoBehaviour { public Transform boidPrefab; public int swarmCount = 100; void Start() { for (var i = 0; i < swarmCount; i++) { Instantiate(boidPrefab, Random.insideUnitSphere * 25, Quaternion.identity); } } }
if (transform.position.magnitude > 25) { velocity += -transform.position.normalized; }
velocity += cohesion + separation * 10 + alignment * 1.5f;
using UnityEngine; public class Boid : MonoBehaviour { public Vector3 velocity; private float cohesionRadius = 10; private float separationDistance = 5; private Collider[] boids; private Vector3 cohesion; private Vector3 separation; private int separationCount; private Vector3 alignment; private float maxSpeed = 15; private void Start() { InvokeRepeating("CalculateVelocity", 0, 0.1f); } void CalculateVelocity() { velocity = Vector3.zero; cohesion = Vector3.zero; separation = Vector3.zero; separationCount = 0; alignment = Vector3.zero; boids = Physics.OverlapSphere(transform.position, cohesionRadius); foreach (var boid in boids) { cohesion += boid.transform.position; alignment += boid.GetComponent<Boid>().velocity; if (boid != collider && (transform.position - boid.transform.position).magnitude < separationDistance) { separation += (transform.position - boid.transform.position) / (transform.position - boid.transform.position).magnitude; separationCount++; } } cohesion = cohesion / boids.Length; cohesion = cohesion - transform.position; cohesion = Vector3.ClampMagnitude(cohesion, maxSpeed); if (separationCount > 0) { separation = separation / separationCount; separation = Vector3.ClampMagnitude(separation, maxSpeed); } alignment = alignment / boids.Length; alignment = Vector3.ClampMagnitude(alignment, maxSpeed); velocity += cohesion + separation * 10 + alignment * 1.5f; velocity = Vector3.ClampMagnitude(velocity, maxSpeed); } void Update() { if (transform.position.magnitude > 25) { velocity += -transform.position.normalized; } transform.position += velocity * Time.deltaTime; Debug.DrawRay(transform.position, separation, Color.green); Debug.DrawRay(transform.position, cohesion, Color.magenta); Debug.DrawRay(transform.position, alignment, Color.blue); } }
using UnityEngine; public class Screenshot : MonoBehaviour { private int count; void Update() { if (Input.GetButtonDown("Jump")) { InvokeRepeating("Capture", 0.1f, 0.3f); } } void Capture() { Application.CaptureScreenshot(Application.dataPath + "/Screenshot" + count + ".png"); count++; } }
Source: https://habr.com/ru/post/182382/
All Articles