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

Click to move vehicle

Discussion in 'Scripting' started by uberalles, Feb 2, 2016.

  1. uberalles

    uberalles

    Joined:
    Jun 29, 2013
    Posts:
    46
    Sorry if this post doesn't really belong in this section.

    I have an RTS style click to move for vehicles that follow normal road rules (staying on right side of road and turning into correct lane) but everything is clunky and 'riding on a rail' as if each vehicle was on a train track (since it basically just checks the distance from the sidewalk and adjusts accordingly. I also have some issues with both setting an exact destination (I just use street/avenue coordinates to get the car to a particular intersection but it gets tricky after that) and with parking. Does anyone know of a good click to move for vehicle tutorial or anyone have any experience in this sort of thing? I really would like to perhaps redo or at least make it feel a little more 'real' while cars are on the road. Thanks.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you'll probably get more buy-in on this topic if you explain the approach that you are currently taking, where you think it is specifically falling short. At the moment that's a very open question... as such it sounds like you want to concentrate on the "traffic simulation" aspect rather than the click/move/navigation.

    i.e. a lower level AI which governs "can i overtake" "can I avoid this guy" etc.
     
  3. uberalles

    uberalles

    Joined:
    Jun 29, 2013
    Posts:
    46
    Good point.

    I'm trying to accomplish (at least right now) a system to move a vehicle from one point (typically from a parked position) to a destination set by a right click anywhere on a road (the car will just park closest to that point). Right now I use a destination of intersections (streets and avenues) meaning I can get the car to (for example) 3rd street and 4th avenue. The final parked destination is a lot trickier. It moves along the road following a ray that 'shoots' to the right and checks for the sidewalk. Each intersection has a collider and notifies the car to turn (based on the destination). After it exits every intersection collider it updates the car's current street/avenue position. Once on the correct block it checks the distance to the destination point and moves off the main road into a parked position. I know I can add some slight variation in the cars movement (side to side) to make it seem less 'riding on a rail' but I'm still wondering if anyone knows a better way to accomplish click to move for vehicles on a road.
     
  4. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    It sounds like you are already on the right path, (pun not intended) What you need is a system of independent control rather than a sky view control.

    You have a track which by some means gets you a path to your destination. This path is marked as points and directions which indicate which road the vehicle should pass. Now, you need to create an AI that guides your vehicle in that direction. To do this, you want to have your vehicle always going towards a target. That target is NOT the end, but the next point in your system. It drives towards that point until it reaches a radius for that point and then gets the next point. (waypoint system)

    The AI will need to be at least away of the angle towards the target and if the target is on the left or right as well as if the target is a greater angle than 80 degrees.

    1) if the target is left or right, it turns the wheel based on the angle, up to its maximum wheel turn angle.
    2) if the target is greater than 80 degrees, it turns the wheel in the opposite angle at maximum turn angle and reverses the car. This will turn the car back towards its target.
     
  5. uberalles

    uberalles

    Joined:
    Jun 29, 2013
    Posts:
    46
    To be honest you've pretty much described what I'm already doing (and that's reassuring that perhaps what I have is the best way of doing this). Each intersection acts a way point and is the car's next destination until it gets to the correct intersection then it becomes an issue of getting the right parking spot (or drive past if the user has clicked the center road and not next to the curb). I think the other issue is determining the street/avenue based on the user's click. I'm thinking of breaking up each 'block' and having it's own name (it's street/avenue and closest intersection - not sure how that part works to be honest...for example how if I click on 5th street how do I know where on 5th it is? Say it's in between 2nd Ave and 3rd Ave, how do I name that block, I guess I could just take the lower (or more southern ave) for every name (so it becomes 5th street and 2nd ave). Any thoughts if there could be a better way to get to the final destination and how to determine where the user clicked?

    Thanks for the reply, any input is always appreciated.
     
  6. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    At some point you have gotten a list of intersections or points on a map that make up a street, lanes, etc. Each of those points should contain nodes which give you direction and traffic configuration. Each node should also contain a street name.

    So why not use the power of LINQ...
    Code (csharp):
    1.  
    2. // get a list of nodes for that street
    3. Node[] nodes = cityNodes.Where(n => n.streetName.Contains("5th")).ToArray();
    4.  
    5. // get a node close to a point on a map
    6. Node node = cityNodes.FirstOrDefault(n => (n.position - clickedPosition).SqrMagnitude <= (minClickDistance * minClickDistance));
    7. if(node == null){
    8. Debug.Log("No street found.");
    9. return null;
    10. }
    11.  
    12. // display the name of that node.
    13. Debug.Log(node.streetAddress + " " + node.streetName);
    14. // 123 5th Avenue
    15.  
    16. // node parking spots?
    17. ParkingSpot[] availableSpots = node.parkingSpots.Where(p => !p.isTaken).ToArray();
    18.  
    19. if(availableSpots .Length > 0){
    20. ParkingSpot spot = availableSpots[0];
    21. spot.isTaken = true;
    22.  
    23. return spot;
    24. }
    25.  
    26. // go to next spot.
    27. target = node.NextNode();
    28.  
     
  7. uberalles

    uberalles

    Joined:
    Jun 29, 2013
    Posts:
    46
    Creating a set series of parking spots (nodes) is definitely an interesting idea. Like I said, I can get the car to the right intersection easy enough, but having a spot with a predetermined size ( accommodate any vehicle) just might work for the car to head towards and park.
    I also think adding a slight wavering and even a little suspension movement on the car might help get rid of that 'riding on a rail' feeling.

    Thanks again for your help.
     
  8. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Make sure that the nodes and parking spaces have direction, so that your car can identify how to park in the spot. So say that if spots 1 and 3 are used, but spot 2 is not, he will have to parallel park into the spot, but if 1 and 2 are available, give him preference to 2, so that he doesn't have to parallel park.