Esempio n. 1
0
    void MoveTowardsTarget()
    {
        Vector3 targetDirection = target - transform.position;

        FishMovementUtils.MoveTowardsTarget(rb, targetDirection, speed);

        // face midway between current velocity vector and target: like anticipating a turn
        Vector3 facingDirection = (rb.velocity + targetDirection) / 2;

        FishMovementUtils.TurnToFace(rb, transform, facingDirection);
    }
    Vector3 MoveTowardsTarget()
    {
        // move towards target along XZ plane
        Vector3 targetDirection = target - transform.position;
        Vector3 force           = FishMovementUtils.MoveTowardsTarget(rb, targetDirection, speed);

        force += AdjustAltitude(hoverHeight);

        // face midway between current velocity vector and target: like anticipating a turn
        Vector3 facingDirection = (rb.velocity + targetDirection) / 2;

        FishMovementUtils.TurnToFace(rb, transform, facingDirection);
        return(force);
    }
    Vector3 AdjustAltitude(float targetAltitude)
    {
        // stay close to the terrain along the Y axis
        float distanceToTerrain = GetDistanceToTerrain();
        float hoverError        = distanceToTerrain - targetAltitude;

        if (Mathf.Abs(hoverError) < hoverMargin)
        {
            return(Vector3.zero);
        }
        else
        {
            float signError = Mathf.Abs(hoverError) / hoverError;
            return(FishMovementUtils.MoveTowardsTarget(rb, -Vector3.up, speed * signError * 0.5f));
        }
    }
Esempio n. 4
0
    public void UpdateFish()
    {
        Vector3 targetVector             = (settings.target.position - transform.position) * settings.speed * settings.targetWeight;
        Vector3 collisionAvoidanceVector = Vector3.zero;
        Vector3 alignmentVector          = Vector3.zero;
        Vector3 cohesionVector           = Vector3.zero;
        Vector3 separationVector         = Vector3.zero;

        // keep going in the current direction if no target
        if (settings.targetWeight == 0.0f || IsHeadingForCollision())
        {
            Vector3 collisionAvoidDir = ObstacleRays();
            collisionAvoidanceVector = collisionAvoidDir * settings.speed * settings.avoidCollisionWeight;
        }

        if (numPerceivedFlockmates != 0)
        {
            centreOfFlockmates /= numPerceivedFlockmates;
            Vector3 offsetToFlockmatesCentre = (centreOfFlockmates - transform.position);
            alignmentVector  = avgFlockHeading * settings.speed * settings.alignWeight;
            cohesionVector   = offsetToFlockmatesCentre * settings.speed * settings.cohesionWeight;
            separationVector = avgAvoidanceHeading * settings.speed * settings.separateWeight;
        }


        Vector3 sumVector = targetVector + collisionAvoidanceVector + alignmentVector + cohesionVector + separationVector;

        if (sumVector.magnitude > settings.maxSpeed)
        {
            sumVector = sumVector * (settings.maxSpeed / sumVector.magnitude);
        }

        FishMovementUtils.MoveTowardsTarget(rb, sumVector, settings.speed);
        FishMovementUtils.TurnToFace(rb, transform, rb.velocity);
        FishMovementUtils.RotateUpright(rb, transform);
    }