- Home /
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?
Answer by FLASHDENMARK · Mar 04, 2011 at 06:50 AM
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.
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.
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 ("$$anonymous$$om! " + transform.name + " is touching me!")
THIS SCRIPT IS SO AWESO$$anonymous$$E!!!! THAN$$anonymous$$ YOU!!!!!
Thanks for this script. It has saved me a lot of time.
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
Answer by cmixco · Nov 07, 2010 at 02:49 AM
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
:-)
when i try this script i get an error
Assets/Scripts/Tank AI/$$anonymous$$ove 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'.
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'
Change the following:
var distance ;
var lookAtDistance = 15.0;
$$anonymous$$ake them into.
var distance : float;
var lookAtDistance : float = 15.0;
Should fix the Left hand side issue.
Answer by fingerdepepe · Oct 31, 2014 at 01:03 PM
what is the a.i. script for the game "mummy maze"?
Your answer
Follow this Question
Related Questions
Turret AI Script 1 Answer
I need a turret script 4 Answers
How to implement Aggro into a AI script (Js) 0 Answers
Maze navigation 1 Answer
tornado twins worm game tutorial turret not firing 2 Answers