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

How to measure lane offset

Discussion in 'Scripting' started by parandham03, Jan 18, 2018.

  1. parandham03

    parandham03

    Joined:
    May 2, 2012
    Posts:
    174
    Hi,
    I have a road mesh. I need to calculate the offset distance between centre of the road lane and car.
    I have attached a reference image below. Please help me to solve this.

    CenterOfLane.png

    Thanks in advance.
     
  2. Fido789

    Fido789

    Joined:
    Feb 26, 2013
    Posts:
    343
    I am not 100 % sure, but you can try this:
    Code (CSharp):
    1. var offset = lane.GetComponent<MeshFilter>().mesh.bounds.center.x - car.GetComponent<MeshFilter>().mesh.bounds.max.x;
     
  3. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    Maybe something like this?

    Code (CSharp):
    1. Math.Abs(carCentre.x - laneCentre.x) - (carWidth / 2.0)
     
  4. parandham03

    parandham03

    Joined:
    May 2, 2012
    Posts:
    174
    The road is not straight road. So how do i do this?
     
  5. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    That makes it a bit more complicated.

    How is your road represented?
    A spline or a model or something?
    Do you want the distance to the edge of the car, or is it okay to use the transform center of the car?
     
  6. neoshaman

    neoshaman

    Joined:
    Feb 11, 2011
    Posts:
    6,469
    Depend on how your lane is represented, I had to solve this previously, using a waypoint system. The basics was to find which are the closest waypoint, each waypoint have a reference to the next waypoint:
    - I compute the vector current waypoint to next waypoint
    - I compute the vector current waypoint to car position
    - I use math project to get the waypoint to car projected to waypoint to next
    - I add the projected result to the current waypoint position to find the lane position
    - I compute the vector lane position to car, which give me the vector offset, or you can just compute the offset distance directely.

    It all depend on which way you represent your spline, if you are using quadratic bezier spline you can use:
    http://blog.gludion.com/2009/08/distance-to-quadratic-bezier-curve.html
     
    whileBreak likes this.
  7. parandham03

    parandham03

    Joined:
    May 2, 2012
    Posts:
    174

    My lane is separate mesh with identical number. My road is two way multi-lane road. I can detect the current lane of my car. but its difficult to fine the lane offset. Actually I need to calculate lane offset(distance between car center and lane center) and road offset(distance between car center and road center). I really don't have knowledge about way points.
    Please help me with some sample code to solve this.

    Thanks in advance.
     
  8. neoshaman

    neoshaman

    Joined:
    Feb 11, 2011
    Posts:
    6,469
    How does the car follow the road?
     
  9. parandham03

    parandham03

    Joined:
    May 2, 2012
    Posts:
    174
    Using raycast i can detect the current lane.
     
  10. neoshaman

    neoshaman

    Joined:
    Feb 11, 2011
    Posts:
    6,469
    Yes but I mean how does the car know where to go next? How do it know the direction of the road?
     
  11. parandham03

    parandham03

    Joined:
    May 2, 2012
    Posts:
    174
    Its the player car so the player control the car.
     
  12. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    You need to represent the middle of the road in some way, if you want to get the distance to it. If it's a winding road, there's no way for Unity to know how it looks without you defining it.