Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Make vehicle automatically steer towards path

Discussion in 'Scripting' started by noxoc, Aug 20, 2015.

  1. noxoc

    noxoc

    Joined:
    Dec 5, 2014
    Posts:
    5
    I wan't my vehicle to automatically steer so that the player "only" has to accelerate. It turns out that this seems a bit harder than I initially thought.

    Right now I have a Path defined by `List<Vector3>` , I get the normal of the lookAhead to the path (that's what `NearestPointOnLine()` does). And then I rotate the vehicle accordingly and give it a relative Force forward.

    I'm probably on the wrong track, so any hints or solutions in the right direction are highly appreciated.

    This is what it currently looks like:

    This is what I have:

    Code (CSharp):
    1.         void Update() {
    2.             FollowPath();
    3.  
    4.             if(actions.Accelerate.IsPressed){
    5.                 body.AddRelativeForce(Vector3.forward * acceleration * Time.deltaTime);
    6.             } else {
    7.                 Vector3 vel = body.velocity;
    8.                 body.velocity = Vector3.zero;
    9.                 body.AddRelativeForce(Vector3.forward * vel.magnitude * Time.deltaTime);
    10.             }
    11.  
    12.             body.velocity = Vector3.ClampMagnitude(body.velocity, maxSpeed);
    13.         }
    14.  
    15.         void FollowPath() {
    16.             // predict future location
    17.             prediction = body.velocity;
    18.             prediction.Normalize();
    19.             prediction *= lookAhead;
    20.             prediction += transform.position;
    21.  
    22.             normal = NearestPointOnLine(aimAt.waypoints[0], aimAt.waypoints[1]-aimAt.waypoints[0], prediction);
    23.  
    24.             float distance = Vector3.Distance(prediction, normal);
    25.             if(distance > aimAt.radius) {
    26.                 SteerAtPath();
    27.             } else {
    28.                 Debug.Log("I'm on track");
    29.             }
    30.         }
    31.  
    32.         void SteerAtPath() {
    33.             NearestNormal();
    34.  
    35.             Transform desired = transform;
    36.             desired.LookAt(normal);
    37.             body.MoveRotation(Quaternion.RotateTowards(transform.rotation, desired.rotation, 10f * Time.deltaTime));
    38.         }
    39.  
     
  2. noxoc

    noxoc

    Joined:
    Dec 5, 2014
    Posts:
    5
    I was just told that there's a Standard Unity Asset with a demo of a Car following a path. I'm simply going to use that since it solves many other problems I was not yet aware of.