• 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
1
Question by Jack Eaves · May 14, 2010 at 10:09 AM · aienemy

Enemy AI Script

I opened a question on this about a week ago and had a reply however the script i was given i can't seem to get it to work without constantly getting errors telling me to add ';' in different places which then results in more errors.

I have a script i had taken from the FPS Tutorial that allows my enemy to move to waypoints, spot my character within range and then start attacking. (the script is rather long so wasn't sure if i should post it)

However i want the enemy to use a variety of attacks at random, for example he might either use attack 1 (normal swipe) attack 2 (tail swipe) or attack 3 (special move).

Here is the script that i have from my last question:

var WaitTime:int=10; // betweenattacks

function monsterAttack(){

while (true){

var distance:Vector3=(Player1.transform.position-Enemy.transform.position);

var dis:float=distance.magnitude;

if (dis<100) do_attack(1) else do_attack(2);

yield WaitForSeconds(WaitTime);

if (dis<100) do_attack(2) else do_attack(3);

yield WaitForSeconds(WaitTime);

if (dis<100) do_attack(3) else do_attack(1);

yield WaitForSeconds(WaitTime);

}

}

Any help would be great. Thank you!

EDIT: Fixed it myself, after gaining more scripting knowledge i realised that i was being a fool and needed to add the animations into the 'do_attack' part and it works fine.

Comment
Add comment · Show 2
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 spinaljack · May 16, 2010 at 11:08 PM 0
Share

Post your answer and mark it correct or delete your question to prevent clutter

avatar image Jack Eaves · May 17, 2010 at 04:06 PM 0
Share

I was going to delete it but cant figure out how?

4 Replies

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

Answer by Jack Eaves · May 17, 2010 at 04:08 PM

Fixed it myself:

var distance:Vector3=(player.transform.position-enemy.transform.position); var dis:float=distance.magnitude;

if (hp.health<500) animation.CrossFade("normalattack", 0.3); else animation.CrossFade("secondaryattack", 0.3);

yield WaitForSeconds(delayShootTime); targetGameObject.active = false;

if (hp.health<300) animation.CrossFade("secondaryattack", 0.3); else animation.CrossFade("normalattack", 0.3);

yield WaitForSeconds(delayShootTime); targetGameObject.active = false;

if (hp.health<100 ) yield StartCoroutine("Special");

yield WaitForSeconds(delayShootTime); targetGameObject.active = false; }

function Special () { fire.active = true;

var distance:Vector3=(player.transform.position-enemy.transform.position); var dis:float=distance.magnitude;

if (hp.health<100 ) animation.CrossFade("specialattack", 0.3); else animation.CrossFade("specialattack", 0.3); audio.Play(); }

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
1

Answer by Tracey P · Feb 17, 2011 at 01:34 PM

I need that script can you please post it

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
-1

Answer by Duddits · May 20, 2011 at 03:20 AM

im new and do any of these scripts work for enemys??

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 kgmr333 · Sep 10, 2012 at 11:37 AM

Hi jack,

i'm also having same requirement to my game i.e., an AI enemy script. that enemy wants to attack on a gameobject with different actions.

can u please post the script and explain the same?

thanks in advance...

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Enemy Spawn Script Spawning Infinite Enemies [Help] 2 Answers

How do I get my emeny AI to detect if he sees a player, or one of the good AI? (C#) 3 Answers

Enemy Follow Script Help 2 Answers

Enemy AI scripting 1 Answer


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