How to do it...

Let's start by creating the interface to control the car. This interface is a MonoBehaviour class with public members, so the player and the agents could interact with it easily and seamlessly:

  1. Create a new file and name it CarController, as shown in the following code:
using UnityEngine;public class CarController : MonoBehaviour{  // next steps}
  1. Next, define the member variables, as follows:
public float speed;public float maxSpeed;public float steering;public float maxSteering;public Vector3 velocity;
  1. Implement the Update for making it run and steer:
private void Update(){  transform.Rotate(Vector3.up, steering, Space.Self);  transform.Translate(Vector3.forward * speed * Time.deltaTime, Space.World);}

Now, we will also ...

Get Unity 2018 Artificial Intelligence Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.