AI character not moving

Hi,

I have my AI character, which has AI.js and AIAnimation.js scripts applied to it from the FPS Tutorial. I have set up the waypoints. When it starts, the AI character turns towards a waypoint and starts to walk but he keeps walking on the same spot and does not move forward.

The script for moving from the AI.js script is:

function MoveTowards (position : Vector3) {
	var direction = position - transform.position;
	direction.y = 0;
	if (direction.magnitude < 0.5) {
		SendMessage("SetSpeed", 0.0);
		return;
	}
	
	// Rotate towards the target
	transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
	transform.eulerAngles = Vector3(0, transform.eulerAngles.y, 0);

	// Modify speed so we slow down when we are not facing the target
	var forward = transform.TransformDirection(Vector3.forward);
	var speedModifier = Vector3.Dot(forward, direction.normalized);
	speedModifier = Mathf.Clamp01(speedModifier);

	// Move the character
	direction = forward * speed * speedModifier;
	GetComponent (CharacterController).SimpleMove(direction);
	
	SendMessage("SetSpeed", speed * speedModifier, SendMessageOptions.DontRequireReceiver);
}

function PickNextWaypoint (currentWaypoint : AutoWayPoint) {
	// We want to find the waypoint where the character has to turn the least

	// The direction in which we are walking
	var forward = transform.TransformDirection(Vector3.forward);

	// The closer two vectors, the larger the dot product will be.
	var best = currentWaypoint;
	var bestDot = -10.0;
	for (var cur : AutoWayPoint in currentWaypoint.connected) {
		var direction = Vector3.Normalize(cur.transform.position - transform.position);
		var dot = Vector3.Dot(direction, forward);
		if (dot > bestDot  cur != currentWaypoint) {
			bestDot = dot;
			best = cur;
		}
	}
	
	return best;
}

The code in Start function of AI.js is:

function Start () {
	// Auto setup player as target through tags
	if (target == null  GameObject.FindWithTag("Player"))
		target = GameObject.FindWithTag("Player").transform;

	Patrol();
}

function Patrol () {
	var curWayPoint = AutoWayPoint.FindClosest(transform.position);
	while (true) {
		var waypointPosition = curWayPoint.transform.position;
		// Are we close to a waypoint? -> pick the next one!
		if (Vector3.Distance(waypointPosition, transform.position) < pickNextWaypointDistance)
			curWayPoint = PickNextWaypoint (curWayPoint);

		// Attack the player and wait until
		// - player is killed
		// - player is out of sight		
		if (CanSeeTarget ())
			yield StartCoroutine("AttackPlayer");
		
		// Move towards our target
		MoveTowards(waypointPosition);
		
		yield;
	}
}

Any ides why the AI character is not actually moving and walking just on the spot. The waypoint is visible to the AI character and this is why it turns towards it.

Thanks

Ads

Unless you have missed some code out, you don’t appear to be calling anything in an Update function. You need to call SimpleMove each frame from the Update function in order to get continuous movement, since each call only moves the object a very small amount.

I dont think we need an update function as the Patrol function has a loop which is ongoing and as the character keeps animating it means the movetowards routine is being run but it is just walking on the same place for some reason. I have got the exact same code working in another game, so dont understand why it is not working here. I even imported the robot character from FPS tutorial and applied the scripts to him and even he walks on the same spot…

I have got the character to move if I change the code and comment out the simple move and use the new code to move as below:

		var delta = position - transform.position; 
          delta.Normalize(); 
          
          var moveSpeed = speed * Time.deltaTime; 
      
          transform.position = transform.position + (delta * moveSpeed); 

	// Move the character
	///direction = forward * speed * speedModifier;
	///direction = speed;
	///GetComponent (CharacterController).SimpleMove(direction);

So, it must have been something with the SimpleMove or the way direction is being calculated. I dont mind using the new code, however as it is calculating the position with the Y axis also, I have to make sure I put all the waypoints at the same height as the enemy (which is 200). Otherwise the enemy seems to go through the floor until it gets to the height of the waypoints. Can I disable the movement in Y axis in transform.position?

Also anyone knows why SimpleMove didnt work.

Thanks,

Ads

You just need to set delta.y to zero before normalising it.