Code Snippets from Project AG Racer (This project was made in Unity using C#)

CPU Pathing and designing solutions for boring/bad CPU's:

notes:
This is a function that decides when its called which path the CPU should follow and stores the correct racing lines into a List for the CPU to go through.
When calling this function you can decide if you want to randomize which path the CPU takes or you can give a manual value for testing purposes.

    public void ChooseNewPath(bool random, int path)

    {

        if (random)

            aipath = (Paths)Random.Range(0, 4);

        else

            aipath = (Paths)path;


        Transform transformIdeal = GameObject.Find("Ideal Lines").transform;

        Transform transformMiddle = GameObject.Find("Middle Lines").transform;

        Transform transformAlternateIdeal = GameObject.Find("Ideal Alternate Lines").transform;

        Transform transformAlternateMiddle = GameObject.Find("Middle Alternate Lines").transform;

        Transform transform;


        switch (aipath)

        {

            case Paths.ideal:

                targetLines = idealLines;

                transform = transformIdeal;

                break;

            case Paths.idealAlternate:

                targetLines = idealAlternateLines;

                transform = transformAlternateIdeal;

                break;

            case Paths.middleAlternate:

                targetLines = middleAlternateLines;

                transform = transformAlternateMiddle;

                break;

            default:

                targetLines = middleLines;

                transform = transformMiddle;

                break;

        }

        for (int i = 0; i < transform.childCount; i++)

        {

            targetLines.Add(transform.GetChild(i).position);

        }

        targetedNode = 0;

        agent.SetDestination(targetLines[targetedNode]);

    }

------------------------------------------------------------------------------------------------------
notes:
This is the FixedUpdate() function of my Movement Base Class, both the PlayerMovement and CPUMovement inherit from this base class to calculate their movement. Because this is an Anti-Gravity Racer i wanted to capture a feeling of bounciness, to achieve this i used a RayCast to check how close the ground is every frame. The closer the harder the force up, giving a bouncy/magnetic feeling.

    protected virtual void FixedUpdate()

    {

        RaycastHit hit;

        if (Physics.Raycast(transform.position, Vector3.down, out hit, Mathf.Infinity, boostPad))

        {

            if (hit.distance < hoverHeight * 2)

            {

                Boost();

            }

        }

        else if (Physics.Raycast(transform.position, Vector3.down, out hit, Mathf.Infinity, groundLayer))

        {

            float distanceToGround = hoverHeight - hit.distance;

            antiGravityForce = distanceToGround * Physics.gravity.magnitude;

            if (hit.distance < hoverHeight)

            {

                rb.AddForce(antiGravityForce * magForce * Vector3.up, ForceMode.Force);

            }

            else if (hit.distance > hoverHeight)

            {

                PastHoverHeight();

            }

        }

    }   


    private void OnTriggerStay(Collider other)

    {

        if (other.gameObject.tag == "SlowZone")

        {

            if (thrustFactor > 0.5f)

                thrustFactor -= 0.05f;

        }

        else

        {

            thrustFactor = 1;

        }

    }

-------------------------------------------------------------------------------------------------