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 float isInsideDegree(RedBird bird, GameObject target, float n) { Vector2 dir = target.transform.position - bird.transform.position; float angle = Vector2.Angle(dir, bird.transform.right); if (angle < n) { return(angle); } return(-1f); }
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); }
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); } }
public void ConeCheck(RedBird bird) { GameObject[] allObjects = UnityEngine.Object.FindObjectsOfType <GameObject>(); foreach (GameObject go in allObjects) { if (go.tag == "YellowBird") { float checkDegree = isInsideDegree(bird, go, cone_angle); if (checkDegree != -1) { Vector2 goPos = go.transform.position; float dist = Vector2.Distance(goPos, bird.transform.position); if (dist < dodge_dist) { Flee(bird, go.transform.position); } } } } }
public void checkCollision(RedBird bird) { float angle = 30; angle *= Mathf.Deg2Rad; Vector3 red = (bird.transform.position + bird.transform.right); Debug.DrawLine(bird.transform.position, red, Color.red); float curent_angle = Mathf.Atan2(bird.transform.right.y, bird.transform.right.x); float greenx = (bird.transform.position.x + Mathf.Cos(curent_angle + angle)); float greeny = (bird.transform.position.y + Mathf.Sin(curent_angle + angle)); Vector3 green = new Vector3(greenx, greeny, 0); Debug.DrawLine(bird.transform.position, green, Color.green); float bluex = (bird.transform.position.x + Mathf.Cos(curent_angle - angle)); float bluey = (bird.transform.position.y + Mathf.Sin(curent_angle - angle)); Vector3 blue = new Vector3(bluex, bluey, 0); Debug.DrawLine(bird.transform.position, blue, Color.blue); }