private void Flee(RedBird bird, Vector2 target) { Vector2 accel = (Vector2)bird.transform.position - target; float strength = Mathf.Min(max_accel / accel.magnitude, max_accel); bird.GetComponent <Rigidbody2D>().AddForce(accel.normalized * strength); }
void Separate(RedBird bird, RedBird other_bird) { Vector2 direction = bird.transform.position - other_bird.transform.position; float distance = direction.magnitude; float strength = Mathf.Min(k / distance * distance, max_accel); bird.GetComponent <Rigidbody2D>().AddForce(direction.normalized * strength); }
public void CollisionPredict(RedBird bird) { GameObject[] allObjects = UnityEngine.Object.FindObjectsOfType <GameObject>(); float min_dist = Mathf.Infinity; Vector2 min_bird = new Vector2(); Vector2 min_go = new Vector2(); foreach (GameObject go in allObjects) { if (go.tag == "YellowBird") { Vector2 dp = go.transform.position - bird.transform.position; if (dp.magnitude > consider_dist) { continue; } Vector2 dv = go.GetComponent <Rigidbody2D>().velocity - bird.GetComponent <Rigidbody2D>().velocity; float t_2_closest = -1 * (Vector2.Dot(dp, dv) / (dv.magnitude * dv.magnitude)); Vector2 p_bird_close = (Vector2)bird.transform.position + bird.GetComponent <Rigidbody2D>().velocity *t_2_closest; Vector2 p_go_close = (Vector2)go.transform.position + go.GetComponent <Rigidbody2D>().velocity *t_2_closest; if (Vector2.Distance(p_bird_close, p_go_close) < min_dist) { min_dist = Vector2.Distance(p_bird_close, p_go_close); min_bird = p_bird_close; min_go = p_go_close; } } } if (Vector2.Distance(min_bird, min_go) < dist_tol) { Vector2 accel = min_bird - min_go; float strength = Mathf.Min(max_accel / accel.magnitude, max_accel); bird.GetComponent <Rigidbody2D>().AddForce(accel.normalized * strength); } }
private void AlignToMovement(RedBird bird) { Vector2 direction = (Vector2)bird.GetComponent <Rigidbody2D>().velocity.normalized; float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg; float rot_speed; if (Mathf.Abs(angle) > slowRadius) { rot_speed = max_rot_speed; } else { rot_speed = angle / slowRadius; } bird.transform.localRotation = Quaternion.Slerp(bird.transform.localRotation, Quaternion.AngleAxis(angle, Vector3.forward), Time.deltaTime * rot_speed); }