Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

A.I In and Out Of Vehicles

Discussion in 'Scripting' started by Vampyr_Engel, Dec 11, 2018.

  1. Vampyr_Engel

    Vampyr_Engel

    Joined:
    Dec 17, 2014
    Posts:
    449
    I have been trying to work out how would I get an A.I pilot, Driver to get into a vehicle and drive it and if the player from a menu tells his A.I Friendly pilot to get out his vehicle because he wants to commandeer it or get out of his vehicle and follow him say into a building how would that be done?
     
  2. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,967
    This is really specific, contextual and vague.

    Basically, noone is going to be able to really help you very well given the request. We can probably give you very high level advice, but without seeing copious amounts of code from your project it will be difficult to help.

    From the way you describe it it sounds like you need to go back and follow some AI tutorials first. Have you done the pluggable AI tutorial? I would start with that first:

    https://unity3d.com/learn/tutorials/topics/navigation/intro-and-session-goals?playlist=17105



    If you already have a decent functioning AI system then we can probably help you retrofit it to also cover this use case, but mostly it sounds like you want advice on how to write an entire AI system. In which case I advise you either use an off the shelf AI solution to speed things up, or start learning how to write your own using the link I pasted above.
     
    Antypodish and Lurking-Ninja like this.
  3. Vampyr_Engel

    Vampyr_Engel

    Joined:
    Dec 17, 2014
    Posts:
    449
    Sorry that I didn't reply earlier the thing is if I show you then you might work out what I mean though I still can't decide whether just to have the controls showing in the H.U.D or the player presses C or a Function Key but I would like to have this menu both for a squad and for an individual player NPC
    Player Commands.jpg I had a look at the pluggable A.I and working on a very very crude rudimentary control system I will put up some code but I don't know if it will work
     
  4. Vampyr_Engel

    Vampyr_Engel

    Joined:
    Dec 17, 2014
    Posts:
    449
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. //General Attack
    5. [CreateAssetMenu (menuName = "PluggableAI/Actions/Attack")]
    6. public class AttackAction : Action
    7. {
    8.     public override void Act (StateController controller)
    9.     {
    10.         Attack (controller);
    11.     }
    12.  
    13.     private void Attack(StateController controller)
    14.     {
    15.         RaycastHit hit;
    16.  
    17.         Debug.DrawRay (controller.eyes.position, controller.eyes.forward.normalized * controller.enemyStats.attackRange, Color.red);
    18.  
    19.         if (Physics.SphereCast (controller.eyes.position, controller.enemyStats.lookSphereCastRadius, controller.eyes.forward, out hit, controller.enemyStats.attackRange)
    20.             && hit.collider.CompareTag ("Player"))
    21.         {
    22.             if (controller.CheckIfCountDownElapsed (controller.enemyStats.attackRate))
    23.             {
    24.                 controller.tankShooting.Fire (controller.enemyStats.attackForce, controller.enemyStats.attackRate);
    25.             }
    26.         }
    27.     }
    28. }
    29. //Specfic Target to attack
    30. [CreateAssetMenu (menuName = "PluggableAI/Decisions/ActiveState")]
    31. public class ActiveStateDecision : Decision
    32. {
    33.     public override bool Decide (StateController controller)
    34.     {
    35.         bool chaseTargetIsActive = controller.chaseTarget.gameObject.activeSelf;
    36.         return chaseTargetIsActive;
    37.     }
    38. }

    Something like that but I want to be able to use keyboards to communicate orders