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
- 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 ![]()
btw: i’m not using the variable “oldPos” atm, so don’t care of it ![]()
//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;
}
}