the enemy chases the player fine, but the enemy will be stuck on to the player, which is not what i wanted. how can i make it such that when its close to the player, it can keep the previous direction vector and let it keep moving using that vector to fly pass the player.
sorry if this was a repost hopefully some one can guide.
thanks.
There are a number of ways that you could conceivably do this. Here’s a simple method. This freezes the target direction when the enemy gets within a certain range and unfreezes it when the move outside of it.
var innerDistance : float;
var outerDistance : float;
private var moveDirection : Vector3;
private var isDirectionFrozen = false;
function Start () {
innerDistance *= innerDistance;
outerDistance *= outerDistance;
//we use sqrMagnitude so we need to square the magnitude.
//we also do it so you don't have to keep track of it in the inspector.
}
function Update () {
var offset = currentTarget.position - transform.position;
if ( isDirectionFrozen ) {
if ( offset.sqrMagnitude > outerDistance ) {
moveDirection = offset.normalized;
isDirectionFrozen = false;
}
}
else {
if( offset.sqrMagnitude > innerDistance ) {
moveDirection = offset.normalized;
}
else {
isDirectionFrozen = true;
}
}
transform.Translate( moveDirection + MaxHeight) * walkSpeed * Time.deltaTime );
}
So here’s what’s supposed to happen. The enemy always moves in the direction of the player. When the get closer than the inner ring, their direction is frozen. That is to say they will move in the same direction until the get beyond the outer ring. then they are free to move as the please until the get within the inner ring again.
BTW, I’m not sure what MaxHeight is so hopefully you have used it correctly.