x


enemy AI script

I need an example of an enemy AI scipt, i can make a turret thanks to Tornado Twins and i assume i just put a transform position on it after the look at script to get it to follow but how would i do a senser for if my player is in the radius of like 40 then trigger it examples?

more ▼

asked Nov 07 '10 at 01:02 AM

Travis 2 gravatar image

Travis 2
97 17 20 24

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

If I understand your question right, then you might be able to use this:

    var distance;
    var target : Transform;    
    var lookAtDistance = 15.0;
    var attackRange = 10.0;
    var moveSpeed = 5.0;
    var damping = 6.0;
    private var isItAttacking = false;

    function Update () 
    {
    distance = Vector3.Distance(target.position, transform.position);

    if(distance < lookAtDistance)
    {
    isItAttacking = false;
    renderer.material.color = Color.yellow;
    lookAt ();
    }   
    if(distance > lookAtDistance)
    {
    renderer.material.color = Color.green; 
    }
    if(distance < attackRange)
    {
    attack ();
    }
    if(isItAttacking)
    {
    renderer.material.color = Color.red;
    }
}


function lookAt ()
{
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
}

function attack ()
{
    isItAttacking = true;
    renderer.material.color = Color.red;

    transform.Translate(Vector3.forward * moveSpeed *Time.deltaTime);
}

When you come close this "AI" it will look at you. If you get even closer it will follow you around until you run away from it. Just drag the target you want the AI to follow inside the slot in the Inspector named "target".

Be aware that this is only a simple AI, you might need to change things inside the script or compine what TornadoTwins has thought you in order for this to suite your needs.

Hope this has helped you.

more ▼

answered Mar 04 '11 at 06:50 AM

OrangeLightning gravatar image

OrangeLightning
5.3k 47 57 111

OL: Just thought you might be interested to know I plugged this script into one of my characters this morning after copying it out last night and it picked right up without error. Will take some tweeking, yes, but something simple was what I was looking for. Yours came up first and looks like the search ended. It'll save me days of original composition and frustration. Thank you.

May 31 '12 at 10:59 AM BillEss

Just to add to that, later on I find myself wrestling with what looks like a common hurdle out there, detecting collisions by other objects with the player controller (when it's not moving.) For anyone else looking at OnPlayerColliderHit or raycasts or various other codey gymnastics to achieve that, the script in this answer may be a quick and efficient approach. Change distance to float and:

if (distance < .3){ print ("Mom! " + transform.name + " is touching me!")

Jun 13 '12 at 12:52 PM BillEss

THIS SCRIPT IS SO AWESOME!!!! THANK YOU!!!!!

Dec 31 '12 at 06:56 PM Lost_C4

Thanks for this script. It has saved me a lot of time.

Feb 13 at 07:55 PM Cornotiberious

I tried to use this script and what happened was my Enemy began spinning around my character at a high speed and i had no idea how to fix this

Apr 30 at 07:19 AM fjcym11
(comments are locked)
10|3000 characters needed characters left

use a sphere collider and check the isTrigger option. then make the collider as wide as you need it for your radius. then use the onTriggerEnter, onTriggerStay, and onTriggerExit for your on/off or whatever you need. http://unity3d.com/support/documentation/ScriptReference/Collider.html

:-)

more ▼

answered Nov 07 '10 at 02:49 AM

cmixco gravatar image

cmixco
47

when i try this script i get an error

Assets/Scripts/Tank AI/Move to player.js(15,17): BCE0051: Operator '<' cannot be used with a left hand side of type 'Object' and a right hand side of type 'float'.

Apr 03 '12 at 03:50 AM Rhys_Pieces
(comments are locked)
10|3000 characters needed characters left

did this get answered.. i get the same error Operator '<' cannot be used with a left hand side of type 'Object' and a right hand side of type 'float'

more ▼

answered Dec 13 '12 at 03:59 AM

trinitysj96 gravatar image

trinitysj96
11 2 5

Hi There.

Please don't post comments as answers. Post comments by clicking the [add new comment] button, a window then open for you to type in. Answer fields are for answers only, as this is a knowledge base.

You can convert this answer to a comment (or just edit your original question), you'll also get a better chance of getting an actual answer if the main list shows none or one answer in blue =]

Under the answer where it says edit | delete | more , click on more , then convert to comment

Dec 13 '12 at 03:59 AM alucardj

sorry bout that.. was in the wrong box..

Dec 13 '12 at 05:09 AM trinitysj96
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3316
x954
x172
x44

asked: Nov 07 '10 at 01:02 AM

Seen: 14034 times

Last Updated: Apr 30 at 07:21 AM