hi there i am wondering if someone can help figure out why when i attach this AI script to an enemy, and set it’s Max follow distance to something low (say 2 or 5), the enemy still moves towards the player object even if it is quite out of the enemy’s max distance value.
here’s my code
using UnityEngine;
using System.Collections;
public class AirEnemyAI : MonoBehaviour {
public Transform target;
public int moveSpeed;
public int rotationSpeed;
public int maxDistance;
private Transform myTransform;
void Awake() {
myTransform = transform;
}
// Use this for initialization
void Start () {
GameObject go = GameObject.FindGameObjectWithTag("Player");
target = go.transform;
maxDistance = 2;
}
// Update is called once per frame
void Update () {
Debug.DrawLine(target.position, myTransform.position, Color.yellow);
//Look at target
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
if(Vector3.Distance(target.position, myTransform.position) > maxDistance) {
//Move towards target
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
}
}
and here is a beta demo of my project, little top down bird bomber game. http://www.drhollandphotography.com/pigeon/WebPlayer.html please excuse the crappy opening screen, it’s just placeholders at the moment, but you get the point 
as you can see the enemies follow the player no matter what distance the player is at.
anyone eble to help me get my maxdistance working? many thanks!
You’re moving towards the target when it’s more than ‘max distance’ away, not less.
setting the max distance to less than the myTransform.position instead of greater than has excactly the same effect. the enemies move towards the player now matter what the max distance is set to. it’s like it ignores it totally.
the only situation in which maxdistance does anything is if i set it to 0. then nothing moves at all. any other value and everything is always constantly moving towards the player
Try doing some debugging. For starters, for each update, print out the ‘distance’ and ‘max distance’ values (it’s likely that’ll give you some hint as to what the problem is, or at least lead you in the right direction).
so Debug.Log(maxDistance), and Debug.Log(target.position) ??
so as i am looking at this code there is nothing to tell it NOT to move when the max distance is greater than the distance set (i’ve changes it to less than transform.position)
is that why? does there need to be a line of code that tells it NOT to move if maxdistance is out of range? because there’s really nothing telling it not to do that at the moment.
what about an else statement right underneath the if that checks maxdistance that would keep the object from moving if maxdistance is to far away?
maybe?
If you write (pseudocode):
if (distance < maxDistance) {
// Move...
}
Then the ‘move’ code will only be executed when the distance is less than the max distance, which I gather is what you want. (In other words, you don’t have to ‘tell it’ not to move; it will simply not move whenever the conditional evaluates to false.)
I’d print out the distance (rather than the target position), so that you can see why the conditional isn’t evaluating as you expect it to.
Debug.Log(maxDistance); under the update function does not output anyhting… there is no log being written, nothing being printed on the screen or in the debug window.
uhg
so what’s wrong when the debug statement does not output any value?
does that mean that for some reason maxDistance is being ignored altogether? that’s what it’s acting like
first of all change the MaxDistance value from 2 to 20 and check if it makes a difference. if your enemies are moving that means your update function works fine I don’t know why the debug statement does not output any values. I can’t tell you anything specific without seeing the actual code.
the code is posted up there.
and no, that changes nothing
I meant the actual project. The code you posted works perfectly fine it must be something outside
yeah i’m pretty sure it’s something wrong with this paticular scene… it’s starting to act oddly. (in the editor) when i play the scene and go to press the spacebar (the bomb button) for some reason now it greys out both the play and pause buttons for the scene, and stops the playback of the scene… it also won’t let me start up the scene again, so i have to close the program and start it again… but as soon as i press the spacebar it stops it again!!! what the hell just happened??? it’s only this scene
That’s really weird. I guess there is something wrong with the editor. otherwise your code should work just fine.
damn i need to know why pressing spacebar is screwing it up like that. that’s preventing me from working on the whole scene altogether.
it’s like it disables playback altogether until i restart the program. man this is irritating
have hat figured out…
still don’t know why my maxdistance isn’t working… there is nothing else in the scene that is tagged as player besides the player, so they wouldn’t be looking at any other object…
Create a single script with only an Update() function, and in that function, print ‘Updating…’ or something. Then, attach that script to an object in the scene, and press play. Whatever you’re printing out, you should see it in the bar at the bottom of the editor window, or in the console when you open it.
If that works, do the same thing, but in the Update() function for your AI agent, to make sure the function is getting called. Then work progressively from there to further isolate the actual cause of the problem.
If you really get stuck, post a minimal project demonstrating the problem, and someone might be able to take a look at it.
(Using the debugger would be another option.)
If your Debug statement is not appearing it means
a) Your script is not active (make sure the check-box in the upper left corner of the script component in the inspector is checked.
b) Your script encounters an error before/during the Debug statement. But you would see this error in the Debug window.
c) You have saved multiple versions of this script and the one attached in your scene is an old version. Use the asset window to search for the name of your script and check for multiple copies. If there’s only one, open the script by double-clicking on the script icon in the script component of your GameObject.
well. there’s only 1 version of the script used or saved anywhere in the project… Debug.Log(maxDistance); right under the update function still outputs nothing at all…
and there is a version of the project posted up at the top of the thread o you can actually see hat’s going on. the enemies follow no matter what distance the player is at… even when i take a look at the scene view and look at enemies that are hundreds of meters away, they are still moving towards the player, and their maxDistance is set to a maximum of 2 meters.so they shouldn’t be moving at all until the player comes within 2 meters of the enemy objects.