Beispiel #1
0
    void FixedUpdate()
    {
        Vector2 pursueForce = follow.get(leaderPos, rb.velocity, targetVel);
        Vector2 cgForce     = congregate.get(cgPos, rb.velocity);
        Vector2 sepForce    = separate.get(Vector2.zero, rb.velocity);

        Debug.DrawRay(transform.position, sepForce, Color.red);

        Vector2 force = pursueForce * weights[0] + cgForce * weights[1] + sepForce * weights[2];

        rb.AddForce(force);
        if (rb.velocity.magnitude > maxSpeed)
        {
            rb.velocity = rb.velocity.normalized * maxSpeed;
        }

        float torque = face.get(Mathf.Atan2(rb.velocity.y, rb.velocity.x), rb.angularVelocity * Mathf.Deg2Rad);

        rb.AddTorque(torque);
        if (Mathf.Abs(rb.angularVelocity) * Mathf.Deg2Rad > maxOmega)
        {
            rb.angularVelocity = Mathf.Sign(rb.angularVelocity) * maxOmega * Mathf.Rad2Deg;
        }
    }