• 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 QueenMab051 · Nov 04, 2010 at 02:34 AM · ai

Basic AI Script Help

Hi I just need some help with AI scripting in Unity 3. My class is creating a game demo and we need to come up with the AI scripts that will make the enemy (a) follow the player and (b) attack the player if in range. Some of the enemies have multiple attacks so I need a script that will allow them to choose between them as well. I've checked out many of the other posts and tutorials but found them to be little help. Thanks!

Comment
Add comment · Show 5
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 Atnas1010 · Nov 04, 2010 at 03:00 AM 0
Share

What do have you so far? This question is really big.

avatar image QueenMab051 · Nov 04, 2010 at 03:03 AM 0
Share

All we have so far is the scripts for character movement and death, and we can get the enemy to start following the player if it gets close enough, but it stops if we get a certain distance away. I'd post the code but my desktop is being a piece at the moment.

avatar image Atnas1010 · Nov 04, 2010 at 03:08 AM 1
Share

Well, for anyone to tell why the enemy stops following once the distance gets too great, they would need to see the code. Let's see if your desktop gets.. assembled?

avatar image Atnas1010 · Nov 04, 2010 at 03:10 AM 0
Share

I assume you've found your way to http://angryant.com/path if they need to be able to navigate obstacles?

avatar image QueenMab051 · Nov 04, 2010 at 03:13 AM 0
Share

Yes I have checked out that tutorial but the pathing script was written by a different student.

1 Reply

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

Answer by Loius · Nov 04, 2010 at 03:26 AM

Making an AI dodge obstacles and/or follow paths is not a simple task; I'd suggest searching the forum for pathfinding and check out an A* (A-star) algorithm or two.

But here's a very very simple implementation. Slap this on an enemy and point its target to the player and it'll chase them in a straight line and play animations named Attack0, 1, 2, etc. based on the numberOfAttacks variable.

var target : GameObject; var speed : float; var range : float; var numberOfAttacks : int;

private var rangeSquared : float;

function Start() { rangeSquared = range*range; }

function Update() { if ( target ) { var moveDirection : Vector3 = target.position - transform.position; moveDirection = moveDirection.normalized speed; transform.position += moveDirection; if ( (transform.position - target.position).sqrMagnitude <= rangeSquared ) { PerformAttack( Random.value numberOfAttacks ); } } }

function DoAttack( attack : int ) { animation.Play( "Attack" + attack ); }

Comment
Add comment · Show 2 · 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 QueenMab051 · Nov 04, 2010 at 03:31 AM 0
Share

Ok I'll give this a try thanks!

avatar image berting01 · Feb 03, 2012 at 03:03 PM 0
Share

how can I put the collision on this script, I have a pacman type game... the object simply pass thru walls :P

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

AI Enemy script 1 Answer

My Zombie Model is coming at me sideways with the AI 1 Answer

Movement around a huge object by avoiding obstacles 1 Answer

Can See Target not working 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