Enemy AI detection logic and tutorials?

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

#pragma strict

var speed : float = 5.0;

function Start () {

}

function Update () {

transform.Translate(Vector3(0,0,speed) * Time.deltaTime);

}

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.

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

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).

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.

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

include();

in php.

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

include();

in php.

Look up either SendMessage or GetComponent

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

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:

// this is code on whatever is checking the player's hidden value

var isPlayerHidden: int = 0; // base value not checked yet


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

// basically says:
//set this var to  the value of hidden in the script on the player object (you determinded above player=Player)
isPlayerHidden = player.GetComponent("NameOfPlayerScriptWithHiddenVariable").hidden;

}

also… my unityscript isn’t the greatest so sorry if there are any errors in syntax and such