Search Unity

Player and AI (NPC) Using The Same Characters And Abilities

Discussion in 'Game Design' started by dadadies, Jul 13, 2022.

  1. dadadies

    dadadies

    Joined:
    Jul 13, 2022
    Posts:
    28
    I assume games in general setup their player characters and AI characters along with their abilities quite differently.

    I want my AIs / NPCs, and even my player character when it is in AI mode (left to AI control), to be able to access the same set of characters and abilities and essentially do what a player can do. The difference being that a player makes the decisions in one case and an AI makes the decisions in the other.

    Currently I have this setup. When player is in control it would use dadadiesInput, when AI is in control it would use dadadiesAI. Both then interact with dadadiesCharacterControl, which is where all the actions of the characters are executed to the game world.

    I dont know how other games do it. Such as fighting games where a player or AI can control the same characters and having access to the same abilities. There are other game examples, such as The Sims game series in which you can control your characters or let AIs control them. Or other games where you can obtain AI characters and control them. I have yet to create a game or prototype to test or figure this out.

    Right now I am coding on a tablet and dont have access to Unity itself to really test anything and am just trying to design and structure my code / scripts, and overall game system on paper basically. I know this sounds really inefficient but I kind of have no choice at the moment as I dont yet have a computer. Iv prototyped game systems in Unity before so I am familiar with Unity and C#.

    Thanks for the time and hopefully there are examples out there showing such a setup. Its possible I am already doing it right but I am interested in how others are doing it, including already published games in the bigger game industry and with indie developers.
     
    Last edited: Jul 13, 2022
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,706
    Search up virtual controllers here on the forum. This post from 2015 talks about one way to use them.

    Briefly, the character's "body" reads from a virtual controller class that could be game-specific -- for example, with values for up, down, left, right, punch, jump, etc. If the virtual controller's jump input is set, the body jumps. The body doesn't need to know what set the jump input.

    On the other side of the virtual controller, for player control attach a script that reads the player's input device to set the virtual controller inputs. For AI control, attach a script that uses some logic to decide which virtual controller inputs to set.

    This way you can separate the execution of the actions (i.e., the body) from the decision-making about which actions to perform.

    Then you'll need to write code that makes those decisions. The nice thing about this approach is that you can completely replace that code with another approach later, without having to touch the virtual controller and body code. I recommend starting with a simple finite state machine (FSM) that does basic movement and punching. Once you get that, and the virtual controller, and player controls, and the body working, you can swap out the FSM implementation for something else. That something else may be a behavior tree system that loads a behavior tree specific to each character's abilities. Or it could be a goal-oriented planning system. The point is that you can experiment with it in isolation now.
     
    angrypenguin and spiney199 like this.
  3. dadadies

    dadadies

    Joined:
    Jul 13, 2022
    Posts:
    28
    I appreciate the info. From what im getting is that you can make your player input and AI input however you like to access the virtual controller (in my case probably my charactercontrol script). Ill check out the links. My brain is partially fried from programming all day; without Unity or an IDE for ease and guidance with my syntax and overall code.
     
    Last edited: Jul 16, 2022