Interactive Media

Mohamed Al-Kaf - Media Explorations

Path Creation Methods for Unity

Written by: Mohamed Al-Kaf | Posted on: | Category:

I found a script on-line. There are two script one place on an empty game object that will draw the path that is desired. The other SCript placed on a game object that will follow the path.

Each path section was a Empty game object. The path was not curved, so to make more Verticies would create a path that looked curved.

Screenshot 2019-04-13 at 15.29.19.png

The problem i am having with this path creation option is the Z axis is also being manipulated i am not sure why. it is possible because of the way the script is written. So the car randomly flips on the z-axis. Even though I have chosen I 2D option in unity. Luckly the rotation direction is correct. so the car follows the path always facing the path and turn when the path changes.

The script used to create the path, using array to add the the to hold all the path points, that are added to the script by placing an empty game object on the scene and then linking it to the script, Adding the total amount of path point in the inspector and then adding the path points manually :

public class EditorPathScript : MonoBehaviour
{

    public Color rayColor = Color.white;
    public List<Transform> path_objs = new List<Transform>();
    Transform[] theArray;

void OnDrawGizmos()
    {
        Gizmos.color = rayColor;
        theArray = GetComponentsInChildren<Transform> ();
        path_objs.Clear ();

        foreach (Transform path_obj in theArray)
        {
            if(path_obj != this.transform)
            {
                path_objs.Add(path_obj);
            }
        for (int i = 0; i < path_objs.Count; i++)
            {
                Vector3 position = path_objs [i].position;
            if (i > 0)
                {
                    Vector3 previous = path_objs[i - 1].position;
                    Gizmos.DrawLine(previous, position);
                    Gizmos.DrawWireSphere(position, 0.3f);                }
            }
        }
    }
}

once the path is set up, attaching another script that will allow the game object. assets to follow the patg. This links the game objcet to the path. Here Are veriables set out, so i can manueally change in the inspector, the change of roation speed, to match the direction of travel when turning, and the speed of travelling on the path. The script also calls the array used in the previous script to know how to follow that path. calculating between the point and tansforming the object between them at the desired speed.

public class MoveOnPath : MonoBehaviour
{
    public EditorPathScript PathToFollow;

    public int CurrentWayPointID = 0;
    public float speed;
    private float reachDistance = .2f; // reach distance smooth movement between hard lines to make curve
    public float rotationSpeed = 5.0f;
    public string pathName;
    //disable loop 
    public GameObject CarFollow;

    Vector3 last_pos;
    Vector3 current_pos;

    void Start()
    {
        // PathToFollow = GameObject.Find(pathName).GetComponent<EditorPathScript>();
        last_pos = transform.position;
    }

    // Update is called once per frame
    void Update()
    {

        float distance = Vector3.Distance(PathToFollow.path_objs[CurrentWayPointID].position, transform.position);
        transform.position = Vector3.MoveTowards(transform.position, PathToFollow.path_objs[CurrentWayPointID].position, Time.deltaTime * speed);
        var rotation = Quaternion.LookRotation (PathToFollow.path_objs[CurrentWayPointID].position - transform.position);
        transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * rotationSpeed);

    if(distance <= reachDistance)
        {
            CurrentWayPointID++;
        }
    if(CurrentWayPointID >= PathToFollow.path_objs.Count)
        {
            CarFollow.GetComponent<MoveOnPath>().enabled = false; // stop at end of path
            //   CurrentWayPointID = 0;  // to loop path 
        }
    }
}