Example #1
0
    private float SetSteeringAngle()
    {
        nearestNode = Respawn.FindNearestNode(trackNode, transform);
        float   farNodeWeightHolder = 1;
        Vector3 targetDirection     = Vector3.zero;

        Debug.DrawRay(frontWheel.transform.position, frontWheel.transform.forward * 2, Color.blue);

        int numberOfNodes = aiSettings.numberNodeInPrediction;

        for (int j = 0; j < aiSettings.numberNodeInPrediction; j++)
        {
            Vector3 nextNode      = trackNode.GetNode(nearestNode + j);
            Vector3 nextDirection = (nextNode - frontWheel.transform.position).normalized;
            nextDirection -= Vector3.Dot(nextDirection, frontWheel.transform.up) * frontWheel.transform.up;

            if (Vector3.Dot(nextDirection, frontWheel.transform.forward) > 0.25)
            {
                targetDirection += nextDirection * farNodeWeightHolder;
                if (j == 0)
                {
                    Debug.DrawRay(frontWheel.transform.position, nextDirection, Color.red);
                }
                else
                {
                    Debug.DrawRay(frontWheel.transform.position, nextDirection);
                }
            }
            else
            {
                numberOfNodes++;
            }

            farNodeWeightHolder *= aiSettings.farNodeWeight;
        }

        targetDirection = new Vector3(targetDirection.x, 0, targetDirection.z).normalized;


        float angle = Vector3.Angle(targetDirection, frontWheel.transform.forward);

        if (Vector3.Dot(targetDirection, frontWheel.transform.right) < 0)
        {
            angle = -angle;
        }

        float resultAngle = Mathf.Lerp(frontWheel.steerAngle, angle, aiSettings.steeringLerpTime * Time.deltaTime);

        if (resultAngle > 45)
        {
            resultAngle = 45;
        }
        else if (resultAngle < -45)
        {
            resultAngle = -45;
        }

        return(resultAngle);
    }