• 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 Blink · Sep 30, 2012 at 11:52 AM · playerenemydistancefollow

Follow within a certain distance?

This script makes my enemy come towards the player when the scene starts, Ive been trying to change it so it only follows me when Im in a certain distance of it. Ive tried over and over but I keep getting errors, Im sure its not that hard to do but if anyone could help me I would be greatfull.

This is the script I haven't tried to change.

 var target : Transform; 
 var moveSpeed = 5;
 var rotationSpeed = 5;
 var myTransform : Transform;
 
 function Awake () {
     myTransform = transform; 
 }
 
 function Start () { 
     target = GameObject.FindWithTag("Player").transform; 
 }
 
 function Update () {
     var dist = Vector3.Distance(target.position, myTransform.position);
     var lookDir = target.position - myTransform.position;
     lookDir.y = 0;
     myTransform.rotation = Quaternion.Slerp( myTransform.rotation,
             Quaternion.LookRotation(lookDir), rotationSpeed*Time.deltaTime );
 
     if(dist > 0.5){
         myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
     }
 }
Comment
Add comment · Show 3
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 Mander · Oct 02, 2012 at 05:48 PM 0
Share

is this even working?

avatar image kmeboe · Oct 02, 2012 at 05:58 PM 0
Share

It might be tough for you to get an answer without more specifics. The best advice I can give you is to get as close as you can on your own, then let us know the specific error you are seeing (from the console, or from the build output in $$anonymous$$ono). Is there a version of your script that works all the time, and it only breaks when you try to add the proximity detection? Specific is good!

avatar image AlucardJay · Oct 02, 2012 at 06:48 PM 0
Share

I just formatted your code, all those spaces ....

Start by Debugging. Add this line before the distance conditional check :

 Debug.Log( "dist " + dist );

does this return any values over 0.5, or indeed over zero? I have a feeling that maybe you have tagged the enemy as "Player" if target.position is not returning null, or is that the errors you mentioned but never posted. Check that you have indeed tagged the player as "Player" !

1 Reply

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

Answer by SergeantBiscuits · Oct 02, 2012 at 07:10 PM

Just add a if(dist < 'maximum distance') qualifier. For instance, if you want your enemy to "aggro" at 10 meters, your Update() function could look like this:

 function Update () {
     var dist = Vector3.Distance(target.position, myTransform.position);
     var lookDir = target.position - myTransform.position;
     lookDir.y = 0;
 
     if(dist < 10){
         myTransform.rotation = Quaternion.Slerp( myTransform.rotation,
             Quaternion.LookRotation(lookDir), rotationSpeed*Time.deltaTime );
 
         if(dist > 0.5){
             myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
         }
     }
 }

You can change that "10" to something like "aggroDist", which you can declare at the top of the script and set to whatever you like from the inspector or what have you.

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 Blink · Oct 03, 2012 at 03:38 PM 0
Share

This works perfectly thanks. This is what I was doing but somewhere I was going wrong.

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

14 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 avatar image avatar image

Related Questions

Move the enemy object opposite to player and keep its distance relative to the player? 1 Answer

Player damage stops working over time 0 Answers

Need help with enemy AI 1 Answer

Enemy is not stopping following the player 2 Answers

Look at closer to player 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges