Enemy obstacle avoidance; needing help!

Hi guys.
I’m starting to work on a really simple obstacle avoidance system for my enemies.
I have a couple problems thou:

1-Even if i Slerp the rotation, the enemy rotation will still “jitter” if it sees obstacles for consequent frames. I think that’s because even if it slerps, the lerp doesn’t complete since the avoidance thing will be called continouosly.

2, and MOOOORE important :smile: - I need a method to avoid interpenetrations between the enemies. I’m corrently using Rigidbodies to make that happen, but they will turn, go upside down and so on when they collide to each other until they settle. This could be cool for one of the types of enemies i need (worm-like) but not so good for others.
And i’d like to avoid rigidbodies if possible to make less calculations when possible.

Anyways, here’s the code.
Tips, cleanups, and solutions really welcome :smile:

btw: i’m not using the variable “oldPos” atm, so don’t care of it :stuck_out_tongue:

//behavioural vars
var avoiding : boolean = false;
var chasing : boolean = false;
//behavioural vars

var lookDistance : int = 6;

var minSpeed = 1;
var maxSpeed = 4;

private var mySpeed;
var myTarget : Transform;

var avoidRotateAngles = 2.0;
var avoidRotSpeed = 2.0;
var rotSpeed = 6.0;

private var monsterPos;
private var oldPos;

function Start()
{
	//search for the player and put its transform as the target
	myTarget = GameObject.FindWithTag("Player").transform;
	
	//randomSpeed
	mySpeed = Random.Range(minSpeed, maxSpeed);
	//at the start, start chasing
	//chasing = true;
}


function Update()
{
	//old position variable, will add the move transform to it to perform the moving action
	oldPos = transform.position;
	
	//raycastHit var, maybe need to move the declaration with the others vars at the top to be able to access it elsewhere
	var hit : RaycastHit;
	
	//FIRST RAYCAST - IN FRONT
	if(Physics.Raycast(transform.position, transform.forward, hit, lookDistance))
	{
		//DEBUG
		Debug.DrawRay (transform.position, transform.forward * lookDistance, Color.green);
		//DEBUG
		
		if(hit.collider.gameObject.tag == "obstacle")
		{
			
			avoiding = true;
			
		}
		
		if(hit.collider.gameObject.tag == "enemy")
		{
			avoiding = true;
		}
	}
	
	//SECOND RAYCAST - RIGHT/2
	if(Physics.Raycast(transform.position, transform.forward + (transform.right/2), hit, lookDistance))
	{
		//DEBUG
		Debug.DrawRay (transform.position, transform.forward + (transform.right/2) * lookDistance, Color.green);
		//DEBUG
		
		if(hit.collider.gameObject.tag == "obstacle")
		{
			
			avoiding = true;
			
		}
		
		if(hit.collider.gameObject.tag == "enemy")
		{
			avoiding = true;
		}
	}
	
	//THIRD RAYCAST - LEFT/2
	if(Physics.Raycast(transform.position, transform.forward + (-transform.right/2), hit, lookDistance))
	{
		//DEBUG
		Debug.DrawRay (transform.position, transform.forward + (-transform.right/2) * lookDistance, Color.green);
		//DEBUG
		
		if(hit.collider.gameObject.tag == "obstacle")
		{
			
			avoiding = true;
			
		}
		
		if(hit.collider.gameObject.tag == "enemy")
		{
			avoiding = true;
		}
	}
	
	else
	{
		chasing = true;
	}
		
	
	if(avoiding)
	{
		//avoiding stuff
		var toRot = Quaternion.Euler(transform.rotation.x,avoidRotateAngles,transform.rotation.z);
		transform.rotation = Quaternion.Slerp(transform.rotation, toRot, Time.deltaTime * avoidRotSpeed);
		avoiding = false;
	}
	
	if(chasing)
	{
		//chasing stuff
		var rotation = Quaternion.LookRotation(myTarget.position - transform.position);
		transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotSpeed);
		monsterPos = oldPos + (transform.forward*mySpeed*Time.deltaTime);
		transform.position = monsterPos;
	}
}

I found a great great tutorial that might help give you a few ideas for obstacle avoidance. I have it indexed at burgzergarcade.com

Hope this helps.

when i was programming my weeping angels i had an issue of them sliding still when i froze their movement - the solution was not only to set their speed to 0, but their velocity aswell.

function OnMouseEnter () 
{
	angel1speed = 0;
	rigidbody.velocity.x = 0;
	rigidbody.velocity.y = 0;
	rigidbody.velocity.z = 0;
}

that could solve the flippy problem depending on what your doing? oh and remember change the function of course :stuck_out_tongue:

Regards, ><>FreakFish<><