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
0
Question by clakes707 · Oct 03, 2012 at 10:50 AM · movementenemy

How to make enemies stop a certain distance away?

Ok so I have three different "enemies" and my player object, which is just the FPS Character Controler from the Standard Assets. The enemies always rotate to face the player, and if they're distance from the player is larger than a certain amount, they move towards him. Here is the code:

     //Inspector variables
     var target : Transform;
     
     
     function Update () 
     {
         transform.LookAt(target);    
         
         if ((Vector3.Distance(transform.position,target.position) > 20) && !((Vector3.Distance(transform.position,target.position)) < 8)) 
         {
             transform.position = Vector3.MoveTowards(transform.position, target.position, .5);
         }
     }


The issues I am having with code are:

  1. The enemies go to the exact point of the player, and eventually push against him so that the player ends up on top of them. I want them to just stop near him, not right on top of him.

  2. Instead of them rotating towards the player all the time, is there a way to simply make them rotate towards their direction? So that I could give them a random direction, and then have them move towards the player(and face that direction) when they spot him?

  3. And my last question is if there is a better way you would have done this? Just for learning purposes.

Thanks for much for your time and help!

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

2 Replies

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

Answer by fafase · Oct 03, 2012 at 10:59 AM

   !((Vector3.Distance(transform.position,target.position)) < 8

I would think this linedoe snot make sense because of operator precedence. ! has higher precedence than < meaning you have !((Vector3.Distance(transform.position,target.position)) done first (which is wrong since Distance returns a float) and then this is compared to 8.

What you want somehow is more likely to be:

  var distance=Vector3.Distance(transform.position,target.position);// no need to perform this operation twice.
  if(distance < 20 && distance > 8)

This will get you guy to move towards you when in the range of 8 and 20. Now you also need to keep at distance with another if statement and move function. See, when the NPC is within 8m it does not move, but I guess you want to move away if you approach him then keeping him at a 8m distance. That is your next task.

For question 2, place the LookAt inside the if statement so that it is looking at your player only when in range. Rest of the time, you coul dhave him going from waypoint to waypoint using an array of...waypoints where the ...waypoint is the target to reach.

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 illinar · Oct 03, 2012 at 12:24 PM

It is recomended to use triggers, in cases like this, since it will be much more efficient than constantly doing distance check. Attach and set up Rigidbodies, Colliders, layers, and tags if you will use them, and then use OnTriggerEnter() and OnTriggerExit() functions.

 bool playerInRange;
 
 void Update ()
 {
    if (playerInRange)
       Atack();
    else
       ApproachPlayer();
 }

 OnTriggerEnter(Collider other)
 {
    if(other.tag == "player")
       playerInRange = true;
 }

 OnTriggerExit(Collider other)
 {
    if(other.tag == "player")
       playerInRange = false;
 }

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 clakes707 · Oct 03, 2012 at 05:30 PM 0
Share

Ok thank you both a lot, these have given me a lot to think I about. I will see what I can do with this and post back if I am still having issues.

avatar image illinar · Oct 06, 2012 at 12:38 PM 0
Share

The reason why you will want to use triggers is because you will need it for detecting hundred different things in many games like RPG. Doors, items, enemies, floor, buttons, other triggers.

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

Compiler error with A.I movement Script 2 Answers

Move enemy ship down while moving left and right 2 Answers

Make an enemy follow along a wall 2 Answers

random direction of enemy when collide whit wall 1 Answer

Jump on Enemy heads to destroy enemy? 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