Exemplo n.º 1
0
    public override CarInput Execute()
    {
        // check if state has any time remaining
        elapsedTime += Time.deltaTime;
        if (elapsedTime >= maxTime)
        {
            StateMachine.ChangeToState <NormalState>();
        }

        // do path follow
        float pathFollowInput = owner.PathFollowInput();

        if (Mathf.Abs(pathFollowInput) > 0)
        {
            // need to turn for path follow, need to reset wander
            wander = 0;
            return(new CarInput
            {
                acceleration = 1,
                turn = pathFollowInput
            });
        }
        else
        {
            // if the opponent is nearby, steer into them
            Vector3 carOffset = owner.otherCar.car.position - owner.car.position;
            if (carOffset.magnitude < 10)
            {
                // try to steer into it
                float dir = Vector3.Dot(owner.car.right, carOffset);
                if (Mathf.Abs(dir) > Mathf.Epsilon)
                {
                    // get sign of direction
                    dir /= Mathf.Abs(dir);

                    // if the target is behind us, slow down
                    float accel = 1;
                    float lead  = Vector3.Dot(owner.car.forward, carOffset);
                    if (lead < 0)
                    {
                        accel = 0.9f;
                    }

                    return(new CarInput
                    {
                        acceleration = accel,
                        turn = dir
                    });
                }
            }

            // apply random wander if we did not turn for any other reason
            wander += Random.Range(-1f, 1f) * wanderStrength * Time.fixedDeltaTime;
            return(new CarInput
            {
                acceleration = 1,
                turn = wander
            });
        }
    }
Exemplo n.º 2
0
    public override CarInput Execute()
    {
        // do path follow
        float pathFollowInput = owner.PathFollowInput();

        if (Mathf.Abs(pathFollowInput) > 0)
        {
            // need to turn for path follow, need to reset wander
            wander = 0;
            return(new CarInput
            {
                acceleration = 0.95f,
                turn = pathFollowInput
            });
        }
        else
        {
            // find closest coin
            Vector3    closest   = Vector3.zero;
            Collider[] colliders = Physics.OverlapSphere(owner.car.position, 10, coinMask);
            foreach (Collider col in colliders)
            {
                if (col.transform.root != owner.transform && (col.transform.position - owner.car.position).magnitude > closest.magnitude)
                {
                    closest = col.transform.position - owner.car.position;
                }
            }

            // try to steer into it
            float dir = Vector3.Dot(owner.car.right, closest);
            if (Mathf.Abs(dir) > Mathf.Epsilon)
            {
                // get sign of direction
                dir /= Mathf.Abs(dir);

                return(new CarInput
                {
                    acceleration = 0.95f,
                    turn = dir
                });
            }

            // apply random wander if we did not turn for any other reason
            wander += Random.Range(-1f, 1f) * wanderStrength * Time.fixedDeltaTime;
            return(new CarInput
            {
                acceleration = 0.95f,
                turn = wander
            });
        }
    }