# Rotate towards node

I’m working on an AI and was wondering what might be the best way to handle keeping an AI in a specific area. So the enemy doesn’t wander too far away from it’s spawn node, if the AI follows the player for too long then have the AI head back to the node, ect. Not sure if once the enemy spawns if I can just create a vector3 to keep track of all this depending on the AI position? Also one more thing. How would you go about rotating the AI towards a node by comparing it’s position to the AI current rotation direction?

the rotation part can be done as follows:

``````dir = (Target.position - transform.position).normalized;
rot = Quaternion.LookRotation(dir);
transform.rotation = Quaternion.Slerp(transform.rotation, rot, Time.deltaTime * RotationSpeed);
``````

then you can use an IEnumerator to maybe check the distance between the enemy and its node (Vector3.distance). Also together with all of this goes bool values that tells the enemy if it must give up etc

Thanks I’ll give it a try

I decided to use a different code but I’m having a slight problem. Since I instantiate my prefab I need to be able to get the Transform from a scene object, I must be doing something wrong because I can’t get the code to apply the objects Transforms as the selection. I was using find gameobject but I might need to use find transform. Maybe you might know the answer.

``````var speed : float;
var isWalking : boolean = true;

var targetObj : Transform;
var point : Vector3 = targetObj.position;

function Start () {

targetObj =  transform.Find("wanderingEnemySpawn");
}

function Update() {

point.y = 0.0;
transform.LookAt(point);

this.transform.Translate(Vector3.forward * speed);

}
``````

Anyone know?

this will need to be in Update

point = targetObj.position;