I have an enemy that has a script to follow the enemy that is enabled when seeing the player and disabled otherwise, this to make sure it doesn’t always follow the player but only moves to the place where it last saw him. The problem is, when reaching its destination it just stands there without doing anything. I want it to activate an idle movement script when moving to the player’s last known position and not seeing him anymore. But the script itself is, as previously mentioned, disabled. So I need to put something in the idle movement script that can react when the position of the player chasing script has been reached. Does anyone know how this is done?
sounds like you are trying to find a workaround to a logic/structure mistake… would make more sense to fix the logic/structure. Can’t help do that without seeing what you are doing…
Post the script… Really hard to understand what is going on.
The Stealth Tutorial has a pretty good example on how to handle the last seen position thing, might be helpful.
Here’s the script that is enabled whenever the player is visible:
#pragma strict
public var target : Transform;
var agent : NavMeshAgent;
function Start ()
{
agent = GetComponent.<NavMeshAgent>();
}
function Update ()
{
agent.SetDestination(target.position);
}
I want it to, whenever it has moved to the player’s last known location and the player is not visible, to switch to this:
public var currentTarget : Transform;
public var targets : Transform[];
public var navigation : NavMeshAgent;
private var i : int = 0;
function Start ()
{
navigation.destination = targets[i].position;
}
function Update ()
{
var dist = Vector3.Distance(targets[i].position,transform.position);
currentTarget = targets[i];
if (dist < 2)
{
if (i < targets.Length - 1)
{
i++; //change next target
navigation.destination = targets[i].position;
}
else
{
i=0;
navigation.destination = targets[i].position;
}
}
}
They work fine separately, and due to the attack script having something enabling and disabling it (the player’s visibility) there’s no problem switching from idle to attack. Switching from attack to idle is trickier.
Also, here’s the script controlling the enemy’s sight.
var freezeScript : FreezeEnemy = Enemy.GetComponent(FreezeEnemy);
var fwd = transform.TransformDirection (Vector3.forward);
var hitInfo : RaycastHit = new RaycastHit();
var canSee : boolean = false;
var sightBlocked : boolean = false;
function Update ()
{
Debug.DrawRay (transform.position, Target.transform.position - transform.position);
var fwd = (Target.position - transform.position).normalized;
if(Physics.Raycast(transform.position + Vector3(0, 1, 0), fwd, hitInfo))
{
if (hitInfo.collider.gameObject.tag == ("Player"))
{
canSee = true;
sightBlocked = false;
}
else
{
canSee = false;
sightBlocked = true;
}
}
if (canSee == true)
{
freezeScript.currentSpotting += 1;
GameObject.Find("Enemy").GetComponent(ChasePlayer).enabled=true;
}
else
{
freezeScript.currentSpotting -= 1;
GameObject.Find("Enemy").GetComponent(ChasePlayer).enabled=false;
}
if (sightBlocked == true)
{
canSee = false;
}
}