Interactive Media

Mohamed Al-Kaf - Media Explorations

Bezier Path Creator - Optimised Paths

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

The Path creating and following method i was using was starting to get frustrating. Luckily i searching the asset store within unity and found another free option.

Screenshot 2019-05-01 at 10.35.44 pm.png

using UnityEngine;

namespace PathCreation.Examples
{
       public class PathFollower : MonoBehaviour
    {
        public PathCreator pathCreator;
        public EndOfPathInstruction endOfPathInstruction;
        public float speed = 5;
        float distanceTravelled;

        void Start() {
            if (pathCreator != null)
            {
                   pathCreator.pathUpdated += OnPathChanged;
            }
        }

        void Update()
        {
            if (pathCreator != null)
            {
                distanceTravelled += speed * Time.deltaTime;
                transform.position = pathCreator.path.GetPointAtDistance(distanceTravelled, endOfPathInstruction);
                transform.rotation = pathCreator.path.GetRotationAtDistance(distanceTravelled, endOfPathInstruction);
            }
        }
        void OnPathChanged() {
            distanceTravelled = pathCreator.path.GetClosestDistanceAlongPath(transform.position);
        }
    }
}

Creating the nodes for the path are self contained and the path can be create as curves. And when linking the car game object it worked smoothly. The the Z access doesnt change.

Screenshot 2019-05-01 at 10.41.07 pm.png

All the paths of the one to follow and the scenery cars.

Screenshot 2019-05-01 at 10.33.08 pm.png