{"id":182690,"date":"2013-06-10T11:12:03","date_gmt":"2013-06-10T07:12:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=182690"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=182690","title":{"rendered":"<span class=\"post_title\">\u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u0443\u0435\u043c Boid&#8217;\u043e\u0432<\/span>"},"content":{"rendered":"<div class=\"content html_format\">   \t<img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage2\/3fc\/046\/a6e\/3fc046a6e6559433dcbce4f6e060ef5d.gif\"\/><\/p>\n<p>  \u0417\u043d\u0430\u0435\u0442\u0435 \u043b\u0438 \u0432\u044b, \u0447\u0442\u043e \u043a\u0443\u0437\u043d\u0435\u0447\u0438\u043a\u0438, \u0431\u0443\u0434\u0443\u0447\u0438 \u0431\u0440\u043e\u0448\u0435\u043d\u043d\u044b\u043c\u0438 \u0432 \u0432\u0435\u0434\u0451\u0440\u043a\u043e, \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0442 \u043c\u0430\u0440\u0448\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e \u043a\u0440\u0443\u0433\u0443 \u043a\u0430\u043a \u043d\u0430 \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u0438 \u0432\u044b\u0448\u0435? \u041f\u0440\u0430\u0432\u0434\u0430 \u0441\u0432\u0435\u0440\u0445\u0443 \u043d\u0435 \u043a\u0443\u0437\u043d\u0435\u0447\u0438\u043a\u0438, \u0430 Boid&#8217;\u044b \u2014 \u043c\u043e\u0434\u0435\u043b\u044c \u043a\u043e\u043b\u043b\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0433\u043e \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u043f\u0442\u0438\u0447\u0435\u043a, \u043f\u0447\u0451\u043b\u043e\u043a, \u0440\u044b\u0431\u043e\u043a \u0438 \u0434\u0440\u0443\u0433\u043e\u0439 \u0436\u0438\u0432\u043d\u043e\u0441\u0442\u0438. \u041d\u0435\u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u043f\u0440\u043e\u0441\u0442\u043e\u0442\u0443 \u043c\u043e\u0434\u0435\u043b\u0438, \u043e\u043d\u0430 \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442 \u044d\u043c\u0435\u0440\u0434\u0436\u0435\u043d\u0442\u043d\u044b\u0435 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430: \u0431\u043e\u0438\u0434\u044b \u0441\u043e\u0431\u0438\u0440\u0430\u044e\u0442\u0441\u044f \u0432 \u043a\u0443\u0447\u0443, \u043b\u0435\u0442\u0430\u044e\u0442 \u0441\u0442\u0430\u044f\u043c\u0438 \u043f\u043e \u043a\u0440\u0443\u0433\u0443, \u043d\u0430\u043f\u0430\u0434\u0430\u044e\u0442 \u043d\u0430 \u043b\u044e\u0434\u0435\u0439.<\/p>\n<p>  \u0412 <a href=\"http:\/\/habrahabr.ru\/post\/182382\/\">\u043f\u0435\u0440\u0432\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u0441\u0442\u0430\u0442\u044c\u0438<\/a> \u043c\u044b \u0431\u0435\u0441\u0445\u0438\u0442\u0440\u043e\u0441\u0442\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043b\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u043e\u0438\u0434\u043e\u0432. \u041f\u0440\u0430\u0432\u0434\u0430 \u043e\u043d \u043f\u043b\u043e\u0445\u043e \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u0441\u043e\u0442\u043d\u0438 \u0431\u043e\u0438\u0434\u043e\u0432 \u043d\u0430\u0448\u0430 \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0430\u0446\u0438\u044f \u043d\u0435 \u0434\u0435\u0440\u0436\u0430\u043b\u0430. \u041f\u0443\u0442\u0451\u043c \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u043c\u0430\u043d\u0438\u043f\u0443\u043b\u044f\u0446\u0438\u0439 \u044d\u0442\u043e \u0447\u0438\u0441\u043b\u043e \u043c\u043e\u0436\u043d\u043e \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0432 \u043f\u0430\u0440\u0443 \u0434\u0435\u0441\u044f\u0442\u043a\u043e\u0432 \u0440\u0430\u0437. \u0412\u0442\u043e\u0440\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0441\u0442\u0430\u0442\u044c\u0438 \u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d\u0430 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0438 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u043c \u0442\u0440\u044e\u043a\u0430\u043c.<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h4>\u041f\u0430\u0440\u043e\u0447\u043a\u0430 \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0439<\/h4>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041d\u0430\u043f\u043e\u043c\u043d\u044e \u043d\u0430 \u0447\u0451\u043c \u043c\u044b \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043b\u0438\u0441\u044c. Boid.cs \u0438\u0437 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0447\u0430\u0441\u0442\u0438 \u0431\u0435\u0437 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0439<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">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(&quot;CalculateVelocity&quot;, 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&lt;Boid&gt;().velocity;              if (boid != collider && (transform.position - boid.transform.position).magnitude &lt; 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 &gt; 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 &gt; 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);     } } <\/code><\/pre>\n<\/div>\n<\/div>\n<p>  \u041d\u0430\u0447\u043d\u0451\u043c \u0441 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043a\u043e\u0441\u043c\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0443\u043f\u0440\u043e\u0441\u0442\u044f\u0442 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0443\u044e \u0440\u0430\u0431\u043e\u0442\u0443 \u0438 \u043f\u0440\u0438\u0431\u043b\u0438\u0437\u044f\u0442 \u043a\u043e\u0434 \u043a \u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u0442\u044c\u0441\u044f \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0439 \u0436\u0438\u0437\u043d\u0438. \u041f\u043e\u043c\u0435\u043d\u044f\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c\u043a\u0443 \u0431\u043e\u0438\u0434\u0430, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0430 \u0431\u044b\u043b\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u043f\u043e\u0445\u043e\u0436\u0430 \u043d\u0430 \u043f\u0442\u0438\u0446\u0443 \u0438 \u0432 \u0442\u043e \u0436\u0435 \u0432\u0440\u0435\u043c\u044f \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043b\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u0442\u0440\u0435\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a\u043e\u0432. \u041f\u0440\u043e\u0441\u0442\u0435\u043d\u044c\u043a\u043e\u0439 \u043f\u0438\u0440\u0430\u043c\u0438\u0434\u043a\u0438 \u0438\u0437 Blender&#8217;\u0430 \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e. \u041a\u0438\u0434\u0430\u0435\u043c \u0444\u0430\u0439\u043b .blend \u0432 \u043f\u0430\u043f\u043e\u0447\u043a\u0443 \u043f\u0440\u043e\u0435\u043a\u0442\u0430, \u0432\u044b\u0434\u0435\u043b\u0430\u0435\u043c \u0432 \u0438\u043d\u0441\u043f\u0435\u043a\u0442\u043e\u0440\u0435 \u0438 \u0432 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430\u0445 \u0438\u043c\u043f\u043e\u0440\u0442\u0430 \u043e\u0442\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u043b\u0438\u0448\u043d\u0435\u0435. \u041a\u043e\u043f\u0438\u0440\u0443\u0435\u043c \u0441\u0442\u0430\u0440\u044b\u0439 \u043f\u0440\u0435\u0444\u0430\u0431 \u0438 \u0434\u0435\u043b\u0430\u0435\u043c \u043d\u043e\u0432\u044b\u0439, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0431\u0443\u0434\u0435\u043c \u0441\u0442\u0430\u0432\u0438\u0442\u044c \u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442\u044b.<\/p>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage2\/b91\/b79\/0df\/b91b790df283c1f2fc018fb5a9e8211a.jpg\"\/><\/p>\n<p>  \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0443 \u043f\u0440\u0435\u0444\u0430\u0431\u0430 \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u044f\u0432\u0438\u043b\u043e\u0441\u044c \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435, \u0432 Update \u0441\u043a\u0440\u0438\u043f\u0442\u0430 \u0441\u0442\u043e\u0438\u0442 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435. \u0414\u043b\u044f \u043f\u043e\u0432\u043e\u0440\u043e\u0442\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0435\u0441\u0442\u044c <a href=\"http:\/\/docs.unity3d.com\/Documentation\/ScriptReference\/Quaternion.RotateTowards.html\">\u043e\u0433\u0440\u043e\u043c\u043d\u043e\u0435<\/a> <a href=\"http:\/\/docs.unity3d.com\/Documentation\/ScriptReference\/Quaternion.Slerp.html\">\u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e<\/a> <a href=\"http:\/\/docs.unity3d.com\/Documentation\/ScriptReference\/Vector3.Slerp.html\">\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432<\/a>, \u043d\u043e \u043c\u044b \u0432\u043e\u0437\u044c\u043c\u0451\u043c <a href=\"http:\/\/docs.unity3d.com\/Documentation\/ScriptReference\/Vector3.RotateTowards.html\">Vector3.RotateTowards<\/a>, \u0438\u0431\u043e \u043e\u043d \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0438 \u043d\u0430\u043c \u0432\u0441\u0451 \u0440\u0430\u0432\u043d\u043e \u0431\u0435\u0437 \u0440\u0430\u0437\u043d\u0438\u0446\u044b. \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u043d\u0443\u0436\u043d\u043e \u043b\u0438 \u0432\u043e\u043e\u0431\u0449\u0435 \u0447\u0442\u043e-\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c, \u043f\u043e\u0442\u043e\u043c \u043f\u043b\u0430\u0432\u043d\u043e \u043f\u043e\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u043c.<\/p>\n<pre><code class=\"cs\">if (velocity != Vector3.zero && transform.forward != velocity.normalized) {     transform.forward = Vector3.RotateTowards(transform.forward, velocity, 10, 1); } <\/code><\/pre>\n<p>  \u0417\u0430\u043e\u0434\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0435\u043b\u0430\u0435\u043c \u043a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0430\u0441\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0431\u043e\u0438\u0434\u044b \u043d\u0430 \u0441\u0446\u0435\u043d\u0435. \u041c\u0443\u0441\u043e\u0440\u0438\u0442\u044c \u0432 \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438 \u2014 \u043f\u043b\u043e\u0445\u0430\u044f \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0430, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u043f\u0440\u044f\u0447\u0435\u043c \u0432\u0441\u0435 \u0431\u043e\u0438\u0434\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <a href=\"http:\/\/docs.unity3d.com\/Documentation\/ScriptReference\/Transform-parent.html\">Transform.parent<\/a>.<\/p>\n<pre><code class=\"cs\">var boid = Instantiate(boidPrefab, Random.insideUnitSphere * 25, Quaternion.identity) as Transform; boid.parent = transform; <\/code><\/pre>\n<p>  <\/p>\n<h4>\u041f\u0440\u0438\u0441\u0442\u0443\u043f\u0430\u0435\u043c \u043a \u0434\u0435\u043b\u0443<\/h4>\n<p>  \u041d\u0430\u0447\u043d\u0451\u043c \u0441 \u0431\u0430\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e. \u0412 \u043d\u0430\u0448\u0435\u043c \u0446\u0438\u043a\u043b\u0435 \u0442\u0440\u0438 \u0440\u0430\u0437\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0432\u044b\u0447\u0438\u0442\u0430\u043d\u0438\u0435 transform.position \u2014 boid.transform.position. \u042d\u0442\u043e \u043f\u043b\u043e\u0445\u043e, \u043b\u0443\u0447\u0448\u0435 \u0437\u0430\u0441\u0443\u043d\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e. \u041d\u0430 \u0441\u043e\u0442\u043d\u0435 \u0431\u043e\u0438\u0434\u043e\u0432 \u044d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043d\u0435 \u0438\u043c\u0435\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u043d\u043e \u043d\u0430 \u043f\u0430\u0440\u0435 \u0442\u044b\u0441\u044f\u0447 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0434\u0430 \u0435\u0449\u0451 \u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437 \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443 \u0440\u0430\u0437\u043d\u0438\u0446\u0430 \u0443\u0436\u0435 \u0431\u0443\u0434\u0435\u0442.<\/p>\n<pre><code class=\"cs\">var vector = transform.position - boid.transform.position; if (boid != collider && vector.magnitude &lt; separationDistance) {     separation += vector \/ vector.magnitude;     separationCount++; } <\/code><\/pre>\n<p>  \u0422\u0430\u043c \u0436\u0435 \u043d\u0435\u043f\u043e\u0434\u0430\u043b\u0451\u043a\u0443 \u0435\u0441\u0442\u044c <a href=\"http:\/\/docs.unity3d.com\/Documentation\/ScriptReference\/Vector3-magnitude.html\">Vector3.magnitude<\/a>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u043a\u043e\u0440\u043d\u044f. \u0414\u043b\u044f \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c <a href=\"http:\/\/docs.unity3d.com\/Documentation\/ScriptReference\/Vector3-sqrMagnitude.html\">Vector3.sqrMagnitude<\/a>. \u0417\u0430\u043e\u0434\u043d\u043e \u043f\u043e\u043c\u0435\u043d\u044f\u0435\u043c magnitude \u0432 \u0444\u043e\u0440\u043c\u0443\u043b\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0432\u0437\u0432\u0435\u0448\u0435\u043d\u043d\u043e\u0433\u043e \u0432\u0435\u043a\u0442\u043e\u0440\u0430, \u044d\u0442\u043e \u043d\u0435 \u0441\u0438\u043b\u044c\u043d\u043e \u043f\u043e\u0432\u043b\u0438\u044f\u0435\u0442 \u043d\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442.<\/p>\n<pre><code class=\"cs\">if (boid != collider && vector.sqrMagnitude &lt; separationDistance * separationDistance) {     separation += vector \/ vector.sqrMagnitude;     separationCount++; } \u2026 if (transform.position.sqrMagnitude &gt; 25 * 25) {     velocity += -transform.position.normalized; } <\/code><\/pre>\n<p>  <\/p>\n<h4>Transform \u0438 GetComponent<\/h4>\n<p>  \u0412 \u043d\u0430\u0448\u0435\u043c \u043a\u043e\u0434\u0435 \u0432\u044b\u0437\u043e\u0432 <a href=\"http:\/\/docs.unity3d.com\/Documentation\/ScriptReference\/GameObject-transform.html\">transform<\/a> \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u0442\u0441\u044f \u0431\u043e\u043b\u044c\u0448\u0435 \u0434\u044e\u0436\u0438\u043d\u044b \u0440\u0430\u0437 \u0438 \u0437\u0430\u0447\u0430\u0441\u0442\u0443\u044e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0432 \u0446\u0438\u043a\u043b\u0435. \u0423\u043c\u043d\u043e\u0436\u0430\u0435\u043c \u044d\u0442\u043e \u043d\u0430 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0431\u043e\u0438\u0434\u043e\u0432 \u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0433\u0440\u0443\u0441\u0442\u043d\u0430\u044f \u043a\u0430\u0440\u0442\u0438\u043d\u0430. \u0417\u0430 \u0434\u043e\u0441\u0442\u0443\u043f\u043e\u043c \u043a transform&#8217;\u0443 \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0441\u043a\u0440\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0434\u043e\u0440\u043e\u0433\u043e\u0439 \u043f\u043e\u0438\u0441\u043a \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430. \u0427\u0442\u043e\u0431\u044b \u044d\u0442\u043e\u0433\u043e \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c, \u0437\u0430\u043a\u0435\u0448\u0438\u0440\u0443\u0435\u043c \u0435\u0433\u043e \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0432\u043e \u0432\u0440\u0435\u043c\u044f <a href=\"http:\/\/docs.unity3d.com\/Documentation\/ScriptReference\/MonoBehaviour.Awake.html\">Awake<\/a>. \u042d\u0442\u043e \u0441\u043e\u0431\u044b\u0442\u0438\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0434\u043e \u043d\u0430\u0447\u0430\u043b\u0430 \u0438\u0433\u0440\u044b \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438. \u0417\u0430\u043e\u0434\u043d\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0432\u044b\u0437\u043e\u0432 \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0430 \u0438\u0437 \u043a\u043e\u043b\u043b\u0430\u0439\u0434\u0435\u0440\u0430 \u043d\u0430 \u0432\u044b\u0437\u043e\u0432 \u043f\u0443\u0431\u043b\u0438\u0447\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0441\u043a\u0440\u0438\u043f\u0442\u0430, \u0430 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u0441 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c \u043a\u043e\u043b\u043b\u0430\u0439\u0434\u0435\u0440\u043e\u043c \u043d\u0430 \u0443\u0441\u043b\u043e\u0432\u0438\u0435 \u0441 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u043e\u043c \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u044f.<\/p>\n<pre><code class=\"cs\">public Transform tr; void Awake() {     tr = transform; } <\/code><\/pre>\n<p>  \u0417\u0430\u043c\u0435\u043d\u0438\u043c \u0432\u0441\u0435 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u043a transform \u043d\u0430 tr.<\/p>\n<pre><code class=\"cs\">foreach (var boid in boids) {     var b = boid.GetComponent&lt;Boid&gt;();     cohesion += b.tr.position;     alignment += b.velocity;      if (vector.sqrMagnitude &gt; 0 && (tr.position - b.tr.position).magnitude &lt; separationDistance)     {         separation += (tr.position - b.tr.position) \/ (tr.position - b.tr.position).magnitude;         separationCount++;     } } <\/code><\/pre>\n<p>  <\/p>\n<h4>\u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u0434\u0430\u043b\u044c\u0448\u0435<\/h4>\n<p>  \u041d\u0443 \u0447\u0442\u043e, \u0443\u0436\u0435 \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043b\u0443\u0447\u0448\u0435, \u043d\u043e FPS \u0432\u0441\u0451 \u0440\u0430\u0432\u043d\u043e \u043f\u0440\u043e\u0441\u0435\u0434\u0430\u0435\u0442, \u043a\u043e\u0433\u0434\u0430 \u0431\u043e\u0438\u0434\u044b \u0441\u0438\u043b\u044c\u043d\u043e \u0441\u0431\u043b\u0438\u0436\u0430\u044e\u0442\u0441\u044f. \u0410 \u0432\u0441\u0451 \u043f\u043e\u0442\u043e\u043c\u0443, \u0447\u0442\u043e <a href=\"http:\/\/docs.unity3d.com\/Documentation\/ScriptReference\/Physics.OverlapSphere.html\">Physics.OverlapSphere<\/a> \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442 \u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0432\u0441\u0451 \u0431\u043e\u043b\u044c\u0448\u0435\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043a\u043e\u043b\u043b\u0430\u0439\u0434\u0435\u0440\u043e\u0432 \u0438 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0442\u0443 \u0436\u0435 \u0441\u0430\u043c\u0443\u044e \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u043f\u0435\u0440\u0435\u0431\u043e\u0440\u0430 \u043f\u043e \u0432\u0441\u0435\u043c \u0431\u043e\u0438\u0434\u0430\u043c.<\/p>\n<p>  <a href=\"http:\/\/en.wikipedia.org\/wiki\/Flocking_(behavior)\">\u0421\u043e\u0433\u043b\u0430\u0441\u043d\u043e \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0443<\/a>, \u043b\u0430\u0441\u0442\u043e\u0447\u043a\u0438 \u0432 \u0441\u0442\u0430\u044f\u0445 \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0432\u0441\u0435\u0433\u043e \u043f\u043e \u043f\u043e\u043b\u0443\u0434\u044e\u0436\u0438\u043d\u0435 \u0441\u043e\u0441\u0435\u0434\u0435\u0439. \u0427\u0435\u043c \u0431\u043e\u0438\u0434\u044b \u0445\u0443\u0436\u0435? \u0411\u0435\u0440\u0451\u043c \u0438 \u0431\u0430\u043d\u0430\u043b\u044c\u043d\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0435\u043c \u0446\u0438\u043a\u043b \u0435\u0449\u0451 \u043e\u0434\u043d\u0438\u043c \u0443\u0441\u043b\u043e\u0432\u0438\u0435\u043c. \u0414\u043b\u044f \u0434\u0432\u0443\u0445 \u0443\u0441\u043b\u043e\u0432\u0438\u0439 \u043b\u0443\u0447\u0448\u0435 \u043f\u043e\u0434\u043e\u0439\u0434\u0451\u0442 \u0446\u0438\u043a\u043b for. \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u0438\u043c\u0435\u0435\u0442 \u0441\u043c\u044b\u0441\u043b \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0442\u044c \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u043e\u0441\u0435\u0434\u0435\u0439, \u043d\u043e \u0438 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435. \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0443\u0441\u043b\u043e\u0432\u0438\u0435 \u0432\u044b\u0445\u043e\u0434\u0430, \u0435\u0441\u043b\u0438 \u043f\u043e\u0431\u043b\u0438\u0437\u043e\u0441\u0442\u0438 \u043d\u0435\u0442 \u0441\u043e\u0441\u0435\u0434\u0435\u0439. \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u043d\u0430\u043c \u043f\u0440\u0438\u0434\u0451\u0442\u0441\u044f \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u043c\u0435\u043d\u0430\u0442\u0435\u043b\u044c \u0432 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0438 \u0432\u0435\u043a\u0442\u043e\u0440\u043e\u0432, \u0438\u043d\u0430\u0447\u0435 \u043f\u0440\u0438 \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0441\u043a\u0443\u0447\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u0441\u043e\u0441\u0435\u0434\u0435\u0439 \u0443 \u0431\u043e\u0438\u0434\u043e\u0432 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0448\u0430\u043d\u0441\u0430 \u0432\u044b\u0431\u0440\u0430\u0442\u044c\u0441\u044f.<\/p>\n<pre><code class=\"cs\">private int maxBoids = 5; \u2026 boids = Physics.OverlapSphere(tr.position, cohesionRadius); if (boids.Length &lt; 2) return; \u2026 for (var i = 0; i &lt; boids.Length && i &lt; maxBoids; i++) {     var b = boids[i].GetComponent&lt;Boid&gt;();     cohesion += b.tr.position;     alignment += b.velocity;     var vector = tr.position - b.tr.position;     if (vector.sqrMagnitude &gt; 0 && vector.sqrMagnitude &lt; separationDistance * separationDistance)     {         separation += vector \/ vector.magnitude;         separationCount++;     } } cohesion = cohesion \/ (boids.Length &gt; maxBoids ? maxBoids : boids.Length); <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0441\u0430\u043c\u0430\u044f \u0433\u043b\u0430\u0432\u043d\u0430\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u2014 \u043c\u044b \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0447\u0430\u0441\u0442\u043e \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u043c \u0432\u0435\u043a\u0442\u043e\u0440 velocity. \u041d\u0435\u043c\u043d\u043e\u0433\u043e \u0448\u0430\u0433\u043d\u0451\u043c \u0432 \u0441\u0442\u043e\u0440\u043e\u043d\u0443 \u0438 \u043d\u0430\u0432\u0435\u0434\u0451\u043c \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0432 \u0438\u043d\u0441\u043f\u0435\u043a\u0442\u043e\u0440\u0435, \u0447\u0442\u043e\u0431\u044b \u0431\u044b\u043b\u043e \u043f\u0440\u043e\u0449\u0435 \u043d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c. \u0421\u0434\u0435\u043b\u0430\u0435\u043c \u0432\u0441\u0435 \u0432\u0430\u0436\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u043f\u0443\u0431\u043b\u0438\u0447\u043d\u044b\u043c\u0438, \u043d\u043e \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043f\u0440\u044f\u0447\u0435\u043c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430 <a href=\"http:\/\/docs.unity3d.com\/Documentation\/ScriptReference\/HideInInspector.html\">HideInInspector<\/a> \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043f\u0430\u0440\u043e\u0447\u043a\u0443 \u043d\u043e\u0432\u044b\u0445. \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 tick, \u043f\u043e\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u0435\u0433\u043e \u0432 \u0432\u044b\u0437\u044b\u0432\u0430\u043b\u043a\u0443 \u043f\u043e \u0442\u0430\u0439\u043c\u0435\u0440\u0443.<\/p>\n<pre><code class=\"cs\">public int turnSpeed = 10; public int maxSpeed = 15; public float cohesionRadius = 7; public int maxBoids = 10; public float separationDistance = 5; public float cohesionCoefficient = 1; public float alignmentCoefficient = 4; public float separationCoefficient = 10; public float tick = 2;  [HideInInspector] public Vector3 velocity; [HideInInspector] public Transform tr; \u2026 InvokeRepeating(&quot;CalculateVelocity&quot;, 0, tick); <\/code><\/pre>\n<p>  \u0414\u0430\u043b\u0435\u0435 \u0434\u0435\u043b\u0430\u0435\u043c \u0444\u0438\u043d\u0442 \u0443\u0448\u0430\u043c\u0438 \u0438 \u0432\u044b\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u0447\u0430\u0441\u0442\u043e\u0442\u0443 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f 2 \u0441\u0435\u043a\u0443\u043d\u0434\u044b. \u0414\u0430-\u0434\u0430, \u0432\u044b \u043d\u0435 \u043e\u0441\u043b\u044b\u0448\u0430\u043b\u0438\u0441\u044c, \u0432 \u0434\u0432\u0430\u0434\u0446\u0430\u0442\u044c \u0440\u0430\u0437 \u0440\u0435\u0436\u0435, \u0447\u0435\u043c \u0443 \u043d\u0430\u0441 \u0431\u044b\u043b\u043e. \u0417\u0430\u043e\u0434\u043d\u043e \u043f\u043e\u0434\u043a\u043e\u0440\u0440\u0435\u043a\u0442\u0438\u0440\u0443\u0435\u043c \u043c\u043d\u043e\u0436\u0438\u0442\u0435\u043b\u0438. \u0422\u0435\u043f\u0435\u0440\u044c \u0432\u043c\u0435\u0441\u0442\u043e \u0441\u043e\u0442\u043d\u0438 \u0431\u043e\u0438\u0434\u043e\u0432 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0442\u044b\u0449\u0443.<\/p>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage2\/7b7\/04d\/f2c\/7b704df2c02bc4cc957d3409b749e36e.jpg\"\/><\/p>\n<h4>\u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043b\u0438-\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043b\u0438, \u0434\u0430 \u043d\u0435 \u0432\u044b\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043b\u0438<\/h4>\n<p>  \u041d\u043e\u0432\u0430\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430. \u0420\u0430\u0437 \u0432 \u0434\u0432\u0435 \u0441\u0435\u043a\u0443\u043d\u0434\u044b \u0443 \u0432\u0441\u0435\u0445 \u0431\u043e\u0438\u0434\u043e\u0432 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u043d\u043e\u0432\u044b\u0445 \u0432\u0435\u043a\u0442\u043e\u0440\u043e\u0432 \u0438 \u043f\u043e\u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0437\u0430\u043c\u0435\u0442\u043d\u0430\u044f \u043f\u0443\u043b\u044c\u0441\u0430\u0446\u0438\u044f. \u042d\u0444\u0444\u0435\u043a\u0442, \u043a\u043e\u043d\u0435\u0447\u043d\u043e, \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0439, \u043d\u043e \u043f\u0442\u0438\u0446\u044b \u0442\u0430\u043a \u043d\u0435 \u0443\u043c\u0435\u044e\u0442. \u0414\u0435\u043b\u0430\u0435\u043c \u0435\u0449\u0451 \u043e\u0434\u043d\u0443 \u043f\u0440\u043e\u0441\u0442\u0443\u044e \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044e \u2014 \u0440\u0430\u0437\u043d\u043e\u0441\u0438\u043c \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u043f\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <a href=\"http:\/\/docs.unity3d.com\/Documentation\/ScriptReference\/Random-value.html\">Random.value<\/a>.<\/p>\n<pre><code class=\"cs\">InvokeRepeating(&quot;CalculateVelocity&quot;, Random.value * tick, tick); <\/code><\/pre>\n<p>  \u041d\u0443 \u0438 \u0447\u0442\u043e\u0431\u044b \u0441\u0442\u0430\u0440\u0442 \u0441\u0438\u043c\u0443\u043b\u044f\u0446\u0438\u0438 \u043d\u0435 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u043b \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0441\u0442\u0440\u0430\u043d\u043d\u043e, \u0432 Awake \u0442\u043e\u0436\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u0441\u0442\u0438 \u0438\u0437 <a href=\"http:\/\/docs.unity3d.com\/Documentation\/ScriptReference\/Random-onUnitSphere.html\">Random.onUnitSphere<\/a>.<\/p>\n<pre><code class=\"cs\">velocity = Random.onUnitSphere * maxSpeed; <\/code><\/pre>\n<p>  \u041f\u0440\u0438\u0433\u043b\u044f\u0434\u044b\u0432\u0430\u0435\u043c\u0441\u044f \u043a \u043d\u0430\u0448\u0435\u043c\u0443 \u043a\u043e\u0434\u0443 \u0435\u0449\u0451 \u0432\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u0435\u0435.<\/p>\n<pre><code class=\"cs\">var b = boids[i].GetComponent&lt;Boid&gt;(); \u2026 var vector = tr.position - b.tr.position; <\/code><\/pre>\n<p>  \u0417\u0430 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0437\u043b\u043e\u0439 \u043c\u0443\u0441\u043e\u0440\u043e\u0441\u0431\u043e\u0440\u0449\u0438\u043a \u0440\u0430\u043d\u043e \u0438\u043b\u0438 \u043f\u043e\u0437\u0434\u043d\u043e \u043e\u0442\u044a\u0435\u0441\u0442 \u0443 \u043d\u0430\u0441 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440. \u0415\u0441\u043b\u0438 \u043c\u044b \u0437\u043d\u0430\u0435\u043c, \u0447\u0442\u043e \u0440\u0435\u0433\u0443\u043b\u044f\u0440\u043d\u043e \u0434\u0435\u043b\u0430\u0435\u043c \u043e\u0434\u043d\u0438 \u0438 \u0442\u0435 \u0436\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435.<\/p>\n<pre><code class=\"cs\">private Boid b; private Vector3 vector; private int i; <\/code><\/pre>\n<p>  \u041d\u0435\u043b\u044c\u0437\u044f \u0442\u0430\u043a\u0436\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0442\u044c, \u0447\u0442\u043e \u0438\u043d\u043e\u0433\u0434\u0430 \u0432\u043c\u0435\u0441\u0442\u043e \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u043a\u043e\u0434\u0430 \u043b\u0443\u0447\u0448\u0435 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u043e\u0433\u0438\u043a\u0443. \u0412 Update \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0441\u0444\u0435\u0440\u044b, \u0433\u0434\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f.<\/p>\n<pre><code class=\"cs\">velocity += -tr.position.normalized; <\/code><\/pre>\n<p>  \u042d\u0442\u043e \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0442\u043e\u0447\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043b\u044f \u0442\u0430\u043a\u043e\u0439 \u0446\u0435\u043b\u0438. \u0415\u0441\u043b\u0438 \u043d\u0430\u043c \u043d\u0443\u0436\u0435\u043d \u043d\u0435 \u0441\u0442\u0440\u043e\u0433\u0438\u0439 \u0435\u0434\u0438\u043d\u0438\u0447\u043d\u044b\u0439 \u0432\u0435\u043a\u0442\u043e\u0440, \u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435, \u0442\u043e \u0432\u0435\u043a\u0442\u043e\u0440 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c.<\/p>\n<pre><code class=\"cs\">velocity += -tr.position\/25; <\/code><\/pre>\n<p>  \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043a\u043e\u0441\u0442\u0438\u0442\u044c \u0435\u0449\u0451 \u043f\u0430\u0440\u0443 \u043c\u0438\u043b\u043b\u0438\u0441\u0435\u043a\u0443\u043d\u0434 \u0440\u0430\u0441\u0447\u0451\u0442\u043e\u0432, \u0435\u0441\u043b\u0438 \u0432\u044b\u043d\u0435\u0441\u0435\u043c \u043f\u043e\u0432\u043e\u0440\u043e\u0442 \u0431\u043e\u0438\u0434\u043e\u0432 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0438 \u0431\u0443\u0434\u0435\u043c \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u043f\u043e \u0442\u0430\u0439\u043c\u0435\u0440\u0443.<\/p>\n<pre><code class=\"cs\">InvokeRepeating(&quot;UpdateRotation&quot;, Random.value, 0.1f); \u2026 void UpdateRotation() {     if (velocity != Vector3.zero && model.forward != velocity.normalized)     {         model.forward = Vector3.RotateTowards(model.forward, velocity, turnSpeed, 1);     } } <\/code><\/pre>\n<p>  <\/p>\n<h4>\u0424\u0438\u0437\u0438\u043a\u0430<\/h4>\n<p>  \u0412\u044b \u043d\u0430\u0432\u0435\u0440\u043d\u043e\u0435 \u0437\u0430\u043c\u0435\u0442\u0438\u043b\u0438, \u0447\u0442\u043e \u043c\u044b \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0444\u0438\u0437\u0438\u043a\u0443, \u0435\u0441\u043b\u0438 \u043d\u0435 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u0432 \u0440\u0430\u0441\u0447\u0451\u0442 \u043f\u043e\u0438\u0441\u043a \u043a\u043e\u043b\u043b\u0430\u0439\u0434\u0435\u0440\u043e\u0432. \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u044d\u043a\u043e\u043d\u043e\u043c\u0438\u0442\u044c \u0435\u0449\u0451 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432, \u0435\u0441\u043b\u0438 \u0432\u044b\u043d\u0435\u0441\u0435\u043c \u0431\u043e\u0438\u0434\u043e\u0432 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u0441\u043b\u043e\u0439, \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043a\u0430\u0442\u044c \u043a\u043e\u043b\u043b\u0430\u0439\u0434\u0435\u0440\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <a href=\"http:\/\/docs.unity3d.com\/Documentation\/ScriptReference\/LayerMask.html\">LayerMask<\/a> \u0438 \u043e\u0442\u043a\u043b\u044e\u0447\u0438\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0441\u0442\u043e\u043b\u043a\u043d\u043e\u0432\u0435\u043d\u0438\u0439 \u043c\u0435\u0436\u0434\u0443 \u0431\u043e\u0438\u0434\u0430\u043c\u0438 \u0432 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430\u0445 \u0444\u0438\u0437\u0438\u043a\u0438.<\/p>\n<pre><code class=\"cs\">public LayerMask boidsLayer; \u2026 boids = Physics.OverlapSphere(tr.position, cohesionRadius, boidsLayer.value); <\/code><\/pre>\n<p>  \u041a\u0443\u0447\u0443 FPS \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0435\u0441\u043b\u0438 \u0432\u044b\u043a\u0440\u0443\u0442\u0438\u0442\u044c \u043d\u0430 \u043c\u0438\u043d\u0438\u043c\u0443\u043c <a href=\"http:\/\/docs.unity3d.com\/Documentation\/ScriptReference\/Physics-solverIterationCount.html\">Solver Iteration Count<\/a> \u0432 <a href=\"http:\/\/docs.unity3d.com\/Documentation\/Components\/class-PhysicsManager.html\">Physics Manager<\/a>. \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0438\u0433\u0440\u0430\u0442\u044c\u0441\u044f \u0441 Fixed Timestep \u0438 Maximum Allowed Timestep \u0432 <a href=\"http:\/\/docs.unity3d.com\/Documentation\/Components\/class-TimeManager.html\">Time Manager<\/a>, \u043d\u043e \u0435\u0441\u043b\u0438 \u0441\u0438\u043b\u044c\u043d\u043e \u0443\u0432\u043b\u0435\u0447\u044c\u0441\u044f, \u0442\u043e \u0441\u0438\u043c\u0443\u043b\u044f\u0446\u0438\u044f \u0441\u0442\u0430\u043d\u0435\u0442 \u0445\u0430\u043e\u0442\u0438\u0447\u043d\u043e\u0439 \u0438 \u043d\u0435\u043f\u0440\u0438\u0432\u043b\u0435\u043a\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0439.<\/p>\n<p>  \u0415\u0449\u0451 \u043e\u0434\u0438\u043d \u043d\u044c\u044e\u0430\u043d\u0441 \u0441\u0432\u044f\u0437\u0430\u043d \u0441 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\u043c. \u041a\u043e\u0433\u0434\u0430 \u043c\u044b \u043f\u043e\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c, \u043c\u044b \u043f\u043e\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u043c \u043f\u0440\u0438\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0439 \u043a \u043d\u0435\u0439 \u0441\u0444\u0435\u0440\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043a\u043e\u043b\u043b\u0430\u0439\u0434\u0435\u0440. \u0414\u043e\u0440\u043e\u0433\u043e \u0438 \u0431\u0435\u0441\u043f\u043e\u043b\u0435\u0437\u043d\u043e. \u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0440\u0435\u0448\u0430\u0435\u0442\u0441\u044f \u043e\u0442\u0434\u0435\u043b\u0435\u043d\u0438\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u0438 \u043e\u0442 \u043a\u043e\u043b\u043b\u0430\u0439\u0434\u0435\u0440\u0430 \u0432 \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438. \u0422\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u0438\u0433\u0440\u0430\u0442\u044c \u0435\u0449\u0451 \u043f\u044f\u0442\u043e\u043a FPS.<\/p>\n<pre><code class=\"cs\">public Transform model; \u2026 if (velocity != Vector3.zero && model.forward != velocity.normalized) {     model.forward = Vector3.RotateTowards(model.forward, velocity, turnSpeed, 1); } <\/code><\/pre>\n<p>  <\/p>\n<h4>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h4>\n<p>  \u041d\u0430 \u044d\u0442\u043e\u043c \u0432\u0441\u0451. \u041e\u0441\u043d\u043e\u0432\u043d\u0443\u044e \u0447\u0430\u0441\u0442\u044c \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432 \u043e\u0442\u044a\u0435\u0434\u0430\u0435\u0442 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u0435 \u043a\u0443\u0447\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0432 \u043a\u0430\u0434\u0440\u0435 \u0438 \u043f\u043e\u0438\u0441\u043a \u0441\u043e\u0441\u0435\u0434\u0435\u0439. \u0421 \u043f\u0435\u0440\u0432\u044b\u043c \u0441\u043b\u043e\u0436\u043d\u043e \u0447\u0442\u043e-\u0442\u043e \u043f\u043e\u0434\u0435\u043b\u0430\u0442\u044c, \u0430 \u0432\u043e\u0442 \u0434\u043b\u044f \u0432\u0442\u043e\u0440\u043e\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u0432\u043e\u043e\u0431\u0449\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0444\u0438\u0437\u0438\u043a\u0443 \u0438 \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0435\u0451 \u043d\u0430 \u0445\u0438\u0442\u0440\u044b\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445, \u043d\u043e \u044d\u0442\u043e \u0443\u0436\u0435 \u0442\u0435\u043c\u0430 \u0434\u043b\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438. \u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u0447\u0442\u043e \u043c\u0443\u0434\u0440\u044b\u0435 \u0445\u0430\u0431\u0440\u0430\u0436\u0438\u0442\u0435\u043b\u0438 \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0430\u0442 \u0441\u0432\u043e\u0438 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0443\u0441\u043a\u043e\u0440\u0435\u043d\u0438\u044f \u0431\u043e\u0438\u0434\u043e\u0432.<\/p>\n<p>  <a href=\"https:\/\/github.com\/BasmanovDaniil\/Boids\">\u0418\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438 \u043d\u0430 GitHub<\/a> | <a href=\"http:\/\/basmanovdaniil.github.io\/Boids\/\">\u041e\u043d\u043b\u0430\u0439\u043d \u0432\u0435\u0440\u0441\u0438\u044f \u0434\u043b\u044f \u043e\u0431\u043b\u0430\u0434\u0430\u0442\u0435\u043b\u0435\u0439 Unity Web Player<\/a>    \t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/182690\/\"> http:\/\/habrahabr.ru\/post\/182690\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">   \t<img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage2\/3fc\/046\/a6e\/3fc046a6e6559433dcbce4f6e060ef5d.gif\"\/><\/p>\n<p>  \u0417\u043d\u0430\u0435\u0442\u0435 \u043b\u0438 \u0432\u044b, \u0447\u0442\u043e \u043a\u0443\u0437\u043d\u0435\u0447\u0438\u043a\u0438, \u0431\u0443\u0434\u0443\u0447\u0438 \u0431\u0440\u043e\u0448\u0435\u043d\u043d\u044b\u043c\u0438 \u0432 \u0432\u0435\u0434\u0451\u0440\u043a\u043e, \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0442 \u043c\u0430\u0440\u0448\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e \u043a\u0440\u0443\u0433\u0443 \u043a\u0430\u043a \u043d\u0430 \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u0438 \u0432\u044b\u0448\u0435? \u041f\u0440\u0430\u0432\u0434\u0430 \u0441\u0432\u0435\u0440\u0445\u0443 \u043d\u0435 \u043a\u0443\u0437\u043d\u0435\u0447\u0438\u043a\u0438, \u0430 Boid&#8217;\u044b \u2014 \u043c\u043e\u0434\u0435\u043b\u044c \u043a\u043e\u043b\u043b\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0433\u043e \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u043f\u0442\u0438\u0447\u0435\u043a, \u043f\u0447\u0451\u043b\u043e\u043a, \u0440\u044b\u0431\u043e\u043a \u0438 \u0434\u0440\u0443\u0433\u043e\u0439 \u0436\u0438\u0432\u043d\u043e\u0441\u0442\u0438. \u041d\u0435\u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u043f\u0440\u043e\u0441\u0442\u043e\u0442\u0443 \u043c\u043e\u0434\u0435\u043b\u0438, \u043e\u043d\u0430 \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442 \u044d\u043c\u0435\u0440\u0434\u0436\u0435\u043d\u0442\u043d\u044b\u0435 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430: \u0431\u043e\u0438\u0434\u044b \u0441\u043e\u0431\u0438\u0440\u0430\u044e\u0442\u0441\u044f \u0432 \u043a\u0443\u0447\u0443, \u043b\u0435\u0442\u0430\u044e\u0442 \u0441\u0442\u0430\u044f\u043c\u0438 \u043f\u043e \u043a\u0440\u0443\u0433\u0443, \u043d\u0430\u043f\u0430\u0434\u0430\u044e\u0442 \u043d\u0430 \u043b\u044e\u0434\u0435\u0439.<\/p>\n<p>  \u0412 <a href=\"http:\/\/habrahabr.ru\/post\/182382\/\">\u043f\u0435\u0440\u0432\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u0441\u0442\u0430\u0442\u044c\u0438<\/a> \u043c\u044b \u0431\u0435\u0441\u0445\u0438\u0442\u0440\u043e\u0441\u0442\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043b\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u043e\u0438\u0434\u043e\u0432. \u041f\u0440\u0430\u0432\u0434\u0430 \u043e\u043d \u043f\u043b\u043e\u0445\u043e \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u0441\u043e\u0442\u043d\u0438 \u0431\u043e\u0438\u0434\u043e\u0432 \u043d\u0430\u0448\u0430 \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0430\u0446\u0438\u044f \u043d\u0435 \u0434\u0435\u0440\u0436\u0430\u043b\u0430. \u041f\u0443\u0442\u0451\u043c \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u043c\u0430\u043d\u0438\u043f\u0443\u043b\u044f\u0446\u0438\u0439 \u044d\u0442\u043e \u0447\u0438\u0441\u043b\u043e \u043c\u043e\u0436\u043d\u043e \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0432 \u043f\u0430\u0440\u0443 \u0434\u0435\u0441\u044f\u0442\u043a\u043e\u0432 \u0440\u0430\u0437. \u0412\u0442\u043e\u0440\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0441\u0442\u0430\u0442\u044c\u0438 \u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d\u0430 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0438 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u043c \u0442\u0440\u044e\u043a\u0430\u043c.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-182690","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/182690","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=182690"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/182690\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=182690"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=182690"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=182690"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}