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. Dismiss Notice

AI controlling vehicles

Discussion in 'Navigation' started by OdyseeGames, Sep 29, 2020.

  1. OdyseeGames

    OdyseeGames

    Joined:
    Jun 24, 2020
    Posts:
    25
    I have AI vehicles (self driving cars) in my game that travel from way point to way point. I want to have AI characters in them, so I can simulate car jacking, like GTA. My questions, do I have the AI character drive the car (so the script is attached to the AI character and not the car) or just be in a self driving car (script attached to car)? Is there really any difference between the two? Also, I have the scripts attached to the cars for the self driving, do I just have those scripts disabled when the Player enters the vehicle or is there a better way than constantly checking if a Player enters the vehicle?
     
  2. Skynet766

    Skynet766

    Joined:
    Sep 13, 2020
    Posts:
    22
    I would have three scripts on the car:
    1. Driving with player input
    2. Driving with AI input
    3. A driving module that (1) and (2) feed into. This is where you handle the "physical result" of (1) or (2). The wheels are rotated 30 degrees. The car is moving at 60km/h. What will the car do in the game world in this frame?
    (1) will probably be pretty simple. The player presses the (A) and the car steers left, for example.

    (2) is where you calculate the car's path to the destination and work out what steering, braking etc is needed to get to the destination. The solution may change over time. If the player rams the AI car, it may have to suddenly reverse to get back onto the path.

    The AI and player should just hook into script (1) or (2). You should not have to enable or disable scripts. The NPC or the player hook into the script and tell it what they want for the car to do. You may have some funny bugs where the car moves where the dead AI told it to, but it is the NPC that must "let go of the wheel"

    The vehicle should be responsible for how to drive it, otherwise each NPC must know how to drive every car when all they care about is "I need to drive to this place" not "how much do I turn the wheel at this intersection".
     
  3. Mjhr

    Mjhr

    Joined:
    Jan 31, 2018
    Posts:
    2
    Please can you tellme how can I doing that using Machine Learning