Bot Walker AI

I’am writing a simple bot AI. Generaly is has few components, on of them is botWalker.

It main task is to deliver Enemy to target point:

	if(Vector3.Distance(transform.position, _target.transform.position) < walkRange)  {
		_target = null;	
		
		return;
	}
	
	
	
	Debug.DrawLine(transform.position, _target.transform.position, Color.red);
	
	
	
 	turnTo(_target.transform.position);
	

	transform.position += transform.forward * AI.walkSpeed * Time.deltaTime; 

Everythig works fine exept that All Enemys can stant into same points (itno each other). Is it some simple way to make it right?

You need some kind of object avoidance…I would use a raycast.

on the enemy:

var avoidDistance : float = 5;

in the Update:

         var fwd = transform.TransformDirection (Vector3.forward);
        var targetDir = transform.TransformDirection(transform.position - _target.transform.position);
        var hit : RaycastHit;
        var hit2 : RaycastHit;
            if (Physics.Raycast (transform.position, fwd,hit, avoidDistance)) 
            if(hit.gameObject.tag == "enemy")
        {
         var relativePoint = transform.InverseTransformPoint(_target.transform.position);
                if(relativePoint > 0.0)
                transform.Rotate(Vector3.left * Time.deltaTime);
        else
         transform.Rotate(-Vector3.left * Time.deltaTime);
    
    }
            else if(Physics.Raycast (transform.position, targetDir,hit2, avoidDistance))
            if(hit2.gameObject.tag == "enemy")
              transform.position += transform.forward * AI.walkSpeed * Time.deltaTime; 
            else if (fwd != targetDir)
            {
         var targetRotation = Quaternion.LookRotation(_target.transform.position - transform.position, Vector3.up);
                    transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0);    
        }
    else
transform.position += transform.forward * AI.walkSpeed * Time.deltaTime;

Untested…

Not yet :slight_smile: Will try it, thx

Seth Bergman, thx for your time, I got this code with your help

public void turnTo(Vector3 point) {
	Vector3 direction  = point - transform.position;

	// Rotate towards the target
    transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(direction), AI.rotationSpeed * Time.deltaTime);
	
	transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
}

private void followHandler() {

	if(Vector3.Distance(transform.position, _target.transform.position) < walkRange)  {
		_target = null;	
		
		return;
	}

	Debug.DrawLine(transform.position, _target.transform.position, Color.red);
	
	Vector3 fwd = transform.TransformDirection (Vector3.forward);
    
	RaycastHit hit;
	
	if(Physics.Raycast(transform.position, fwd, out hit, avoidDistance)) {
		if(hit.collider.gameObject.tag == gameObject.tag) {
			
			transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(Vector3.left), AI.rotationSpeed * Time.deltaTime);
			transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
			
		}
	} else {
		turnTo(_target.transform.position);
		transform.position += transform.forward * AI.walkSpeed * Time.deltaTime;
	}

}

followHandler starting every update. Just two problems left.

  1. Bot has always avoids the barrier on the left
  2. It realy slow choosing direction when a lot barrier’s ahead

The wy to fix it ray cast a lot of rays and chhose direction, but i Think that is not the best solution? probably some body has better ideas?