Search Unity

Enemy AI detection logic and tutorials?

Discussion in 'Scripting' started by Tezelian, Jan 20, 2014.

  1. Tezelian

    Tezelian

    Joined:
    Oct 22, 2013
    Posts:
    256
    So I added a code into my apc (exported from Maya) which translates the apc to positive z axis position giving the illusion that it is moving

    Code (csharp):
    1. #pragma strict
    2.  
    3. var speed : float = 5.0;
    4.  
    5. function Start () {
    6.  
    7. }
    8.  
    9. function Update () {
    10.  
    11. transform.Translate(Vector3(0,0,speed) * Time.deltaTime);
    12.  
    13. }
    Now I want to add a JavaScript for enemy AI detection

    so if player enters in range (specific range) of apc (either trigger or code) then health of player goes down by 10 kind of logic or I can jsut add a debug log for now to see if it's working.

    I want to break this step by step

    I am new to js and I don't know how or where to begin, are there any tutorials for this kind of AI logic?

    If I created a cube on Unity then that cube would come with a box collider and trigger, but objects I export from Maya do not come with a collision box which means I have to create or apply a collision box to the apc myself, but how? Would it be through Components > Physics > Box Collider? and then would I need to parent it to the apc?

    Or maybe I could create a new variable

    something like

    Var boundary : float = 100;

    and if the apc gets to 100 then activate health or something

    But how can I tell the program of the APcs exact position? So let's say the apc starts off at a specific vector or position, can I say if player is <100 that position then trigger something for now I could add a debug.log to see if it's working.
     
  2. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    There is a box you tick to generate colliders when you import a mesh:
    http://docs.unity3d.com/410/Documentation/Components/FBXImporter-Model.html

    Or you can just add a box collider by selecting the object and in the inspector click the 'add component' button and use physics -> box collider
     
    Last edited: Jan 20, 2014
  3. makoto_snkw

    makoto_snkw

    Joined:
    Aug 14, 2013
    Posts:
    340
    Hints:

    1. Use collider as trigger.
    2. Attached a script to the gameobject of that contain the collider.
    3. Find in the doc about OnTriggerEnter, OnTriggerStay and OnTriggerExit.
    4. Those codes in #3, should be inside the script attached to the gameobject. (Enemy).
     
  4. Tezelian

    Tezelian

    Joined:
    Oct 22, 2013
    Posts:
    256
    thank you everybody, I got it working for now

    So if my character enters the sphere of the object that is moving then I get a message saying "apc has detected you" and the health which is 5 goes down to 4, note I debugged this via Log not actually a UI element yet but hopefully I'll get there soon enough.



    var health : float = 5.0;

    function OnTriggerEnter (player : Collider) {
    if(player.tag=="Player")
    health = health - 1;
    Debug.Log("apc has detected you" + health);
    }


    Now my next step is that the enemy detects me if I go into range but I want to add a code saying if my player is crouching then I will not be detected.

    How can I do that?

    I have a crouch script for my fp controller and in that script (which is in my fpcontroller) I made a new variable called "hidden"

    The next question is how can I get variables from other scripts


    because when I press c my player crouches and I made a variable, amde it 1 and I'm saying if player crouches then hidden (which is 1) = hidden + 1 which would equal 2

    so now in my apc script I want to also say if hidden == 2 then.................


    this is my code for the apc character proximtiy detector


    var health : float = 5.0;



    function OnTriggerEnter (player : Collider) {
    if(player.tag=="Player")
    health = health - 1;
    Debug.Log("apc has detected you" + health);
    }


    so below health I want to put if hidden == 2...

    but hidden is in another script.
     
  5. makoto_snkw

    makoto_snkw

    Joined:
    Aug 14, 2013
    Posts:
    340
    MAYBE make hidden as GLOBAL variable?

    I'm looking for a solution to call a function in another script as well, but I'm using C#.

    Perhaps javascript can use some import method to import the script like

    Code (csharp):
    1. include();
    in php.
     
  6. makoto_snkw

    makoto_snkw

    Joined:
    Aug 14, 2013
    Posts:
    340
    MAYBE make hidden as GLOBAL variable?

    I'm looking for a solution to call a function in another script as well, but I'm using C#.

    Perhaps javascript can use some import method to import the script like

    Code (csharp):
    1. include();
    in php.
     
  7. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    Look up either SendMessage or GetComponent
     
  8. Tezelian

    Tezelian

    Joined:
    Oct 22, 2013
    Posts:
    256
    In the run script of the fp controller I made it into a global variable as

    static var hidden: float = 1.0;

    so then I went into my apc script but it couldn't find hidden

    I researched getcomponent and I know how it works but I don't how to use it

    Do I put hidden inside GetComponent like GetComponent(hidden)?

    Do I put that at the top with the variables?

    I will let you guys know if I sort it out
     
  9. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    I'd just avoid the static for something like this and have hidden be a variable on your player stat script or whatever. then what ever needs to know the value of it would be something like this:

    Code (csharp):
    1.  
    2. // this is code on whatever is checking the player's hidden value
    3.  
    4. var isPlayerHidden: int = 0; // base value not checked yet
    5.  
    6.  
    7. //your code from above
    8. function OnTriggerEnter (player : Collider) {
    9. if(player.tag=="Player")
    10. health = health - 1;
    11. Debug.Log("apc has detected you" + health);
    12.  
    13. // basically says:
    14. //set this var to  the value of hidden in the script on the player object (you determinded above player=Player)
    15. isPlayerHidden = player.GetComponent("NameOfPlayerScriptWithHiddenVariable").hidden;
    16.  
    17. }
    18.  
    also... my unityscript isn't the greatest so sorry if there are any errors in syntax and such
     
    Last edited: Jan 23, 2014