Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Travis 2 · Nov 07, 2010 at 01:02 AM · aiturrettornado-twins

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
9
Best Answer

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.

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BillEss · May 31, 2012 at 10:59 AM 0
Share

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.

avatar image BillEss · Jun 13, 2012 at 12:52 PM 0
Share

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!")

avatar image Lost_C4 · Dec 31, 2012 at 06:56 PM 0
Share

THIS SCRIPT IS SO AWESO$$anonymous$$E!!!! THAN$$anonymous$$ YOU!!!!!

avatar image Cornotiberious · Feb 13, 2013 at 07:55 PM 0
Share

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

avatar image fjcym11 · Apr 30, 2013 at 07:19 AM 0
Share

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

Show more comments
avatar image
0

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

:-)

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rhys_Pieces · Apr 03, 2012 at 03:50 AM 0
Share

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

avatar image trinitysj96 · Dec 13, 2012 at 03:59 AM 0
Share

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'

avatar image Siniestrum · Jun 25, 2013 at 01:54 AM 0
Share

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.

avatar image
0

Answer by fingerdepepe · Oct 31, 2014 at 01:03 PM

what is the a.i. script for the game "mummy maze"?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by osama samy · Nov 28, 2014 at 10:05 PM

It says unspected token ! what should i do !

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges