• 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 jhem · Feb 19, 2011 at 04:25 AM · ai

An AI that will move away or move to another waypoint when player goes near?

I am doing this simple project of catching things and I want to have an AI on my project so it will be more challenging. Any tutorials or suggestion you might give me? I want my AI to avoid my player when it comes near. Thanks!

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
0

Answer by sriram90 · Feb 19, 2011 at 05:35 AM

http://unity3d.com/support/resources/tutorials/fpstutorial.html

take look in this FPS Tutorial....it have AutoWayPoint scrit for AI...

in that they just make some wayPoint and those Robot(Enemies) will move automatically towards to that waypoints...change your script as whatever you need...

find the distance between player and enemy....if it's very near you may move 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
0

Answer by Michael 12 · Mar 27, 2011 at 02:36 AM

I got this to work for one of my Alien Zombies that I needed to be a scared Alien Zombie. You'll need to change a few things and add the Waypoint functions but it makes a character run away if you get within a certain distance.

var modelAnimation : Animation; var awareDistance : float = 25.0; var scaredDistance : float = 20.0; var player : Transform;

var runSpeed : float = 15.0;

enum AIStatus {idle = 0, Scared = 1} private var status = AIStatus.idle;

var controller : CharacterController;

private var moveDirection = Vector3.zero;

function Awake() { controller = GetComponent(CharacterController); }

function Update() { CheckStatus();

 switch(status)
 {
     case AIStatus.idle:
     idle();
     break;

     case AIStatus.Scared:
     RunAway();
     break;

 }

}

function idle() { modelAnimation.CrossFade ("idle"); }

function RunAway() { transform.eulerAngles.y = player.transform.eulerAngles.y; moveDirection = Vector3(0,0,40);
moveDirection = transform.TransformDirection(moveDirection); moveDirection = runSpeed; controller.SimpleMove(moveDirection Time.deltaTime);

 modelAnimation.CrossFade ("RunAttack");

}

function CheckStatus() {

 var dist = (player.position - transform.position).magnitude;

 if(dist < scaredDistance)
 {
     status = AIStatus.Scared;
 }

 else if (dist > awareDistance)
 {
     status = AIStatus.idle;
 }

}

Comment
Add comment · Show 1 · 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 pixelone · Sep 14, 2012 at 06:46 PM 0
Share

@$$anonymous$$ichael $$anonymous$$night how would I combine that lovely script above with an array? The idea is to drop this scrip on a scrip manager ( empty game object) and with the array just assign what I want to run away from to the array list via the inspector.

Thanks

avatar image
0

Answer by pixelone · Sep 15, 2012 at 12:43 AM

@Michael Knight how would I combine that lovely script above with an array? The idea is to drop this scrip on a scrip manager ( empty game object) and with the array just assign what I want to run away from to the array list via the inspector.

Thanks

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 ushouldhirejb · Jun 03, 2014 at 03:42 AM

I am new to unity & scripting & I just wanna say after hours/days of frustration that script was a big help for me to understand how to get some npc animal characters to act like AI animals. Good script, thanks for sharing 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

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

NavMesh link does not connect properly on runtime 1 Answer

AI targeting error 2 Answers

How to make AI walk around ingame built walls? 1 Answer

AI Enemy Follow Player 2 Answers

Auto-moving RigidBody2D to a point, using thrusters (part of AI) 0 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