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

Boss AI

Discussion in 'Scripting' started by EeanZ, Nov 12, 2019.

  1. EeanZ

    EeanZ

    Joined:
    Nov 11, 2019
    Posts:
    8
    I want to start learning some AI. So far ive learned to use scripts mainly as object components. I have a very good understanding of classes and objects as well as inheritance and i want to use those for my Boss AI. So my problem now is where do I create my scripts, aside from being components in game objects.

    eg. I have 2 game objects (2 different bosses). both of them have similar properties that i want to put in 1 abstract class(health, damage). then 2 derived classes for their attacks in form of methods. then the game objects will have a script component that uses switch case to determine when to use an attack.

    Where do I create or place those first 2 classes that i mentioned
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,835
    Classes don't have to be attached to game objects. You can just write the script and leave it as a file somewhere in your assets folder, and other scripts will be able to use it for inheritance, etc.

    You can even create classes that don't inherit from MonoBehaviour, and use them as plain C# objects with no relation to UnityEngine to do all the things you can normally do in plain C# outside Unity.
     
    Joe-Censored likes this.
  3. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    I always use base classes that derives from MonoBehaviour for AI or Player classes. I make a scripts folder, have a sub folder for AI or Enemies and put all the classes in there to keep it organized. Sometimes even sub folders for enemy types. I also make one Character/Actor class that has all the properties that Players and AI have like health/damage/equipment and derive from that.
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    This ^^^

    For complex systems I tend to only put code into MonoBehaviours (or classes which inherit from MonoBehaviour) which directly applies to the GameObject in the scene or for interacting with the system, and the inner workings of the system will be in C# classes which don't inherit from MonoBehaviour.
     
  5. EeanZ

    EeanZ

    Joined:
    Nov 11, 2019
    Posts:
    8
    heres the system that i can understand from you guys. so i have a bosses class(health, damage) which derives 2 boss ai classes(attack functions) bossai1 and bossai2.

    and then another script that is derived from monobehaviour will be attached to boss1 for example that will instantiate bossai1 and use that guys functions and setvariables in the start and update functions.

    sounds solid so far, any comments for this?

    Edit: i realize that the bossai with attack functions would probably need to be derived from monobehaviour since they will have movement in them that will use the update
     
    Last edited: Nov 15, 2019