- Home /
Ai Obstacle Avoidance
Essentially I am trying to make an object chase me. I want it to be able to chase me around walls and such. So far the chasing works, but the object still runs into the walls. I watched a video on it, and it seemed to help. Although, the character still eventually ends up floating because it runs into the wall so much. Video I Watched: vimeo.com/9304844
Here is my code - two scripts. Guard Script and sightScript. Guard script checkWall() function is where the main code is for the video.
guardScript:
pragma strict
var sight : Transform; var script : sightScript; var rotationSpeed : int = 8; var moveSpeed : int = 3;
var objectInFront = false;
private var myTransform : Transform; function Awake() { myTransform = transform; script = sight.transform.GetComponent(sightScript); } function Start () {
}
function Update () {
if(script.see)
{
checkWall();
if (!objectInFront)
LookAt();
Move();
}
}
function Move() { myTransform.position += myTransform.forward moveSpeed Time.deltaTime; }
function LookAt() { myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(script.playerPos.transform.position - myTransform.position), rotationSpeed * Time.deltaTime);
}
function checkWall() {
objectInFront = false;
var dir : Vector3 = (script.playerPos.transform.position - transform.position).normalized;
var hit :RaycastHit;
if(Physics.Raycast(transform.position, transform.forward, hit, 20))
{
if(hit.transform != transform)
{
dir += hit.normal*20;
}
Debug.DrawLine(transform.position, hit.point, Color.red);
objectInFront = true;
}
var leftR : Vector3 = transform.position;
var rightR : Vector3 = transform.position;
leftR.x -= 1;
rightR.x += 1;
if(Physics.Raycast(leftR, transform.forward, hit, 20))
{
if(hit.transform != transform)
{
dir += hit.normal*20;
}
Debug.DrawLine(leftR, hit.point, Color.red);
objectInFront = true;
}
if(Physics.Raycast(rightR, transform.forward, hit, 20))
{
if(hit.transform != transform)
{
dir += hit.normal*20;
}
Debug.DrawLine(rightR, hit.point, Color.red);
objectInFront = true;
}
if(objectInFront)
{
var rot : Quaternion = Quaternion.LookRotation(script.playerPos.transform.position - myTransform.position);
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, rot , rotationSpeed * Time.deltaTime);
}
}
sightScript : //Script for detection of player, reactions to detection of player #pragma strict //Inspector Variables var mainObj : Transform; var playerPos : Transform; var see : boolean = false; //Private Variables private var myTransform : Vector3;
private var enterPlayerPos : Vector3;
function Awake() { myTransform = transform.position; } function Start () {
}
function Update () {
if (see) {
}
}
function OnTriggerEnter (other: Collider) { myTransform = transform.position; if (other.gameObject.tag == "Player") { Debug.Log("its working");
var hit: RaycastHit;
if (Physics.Linecast(myTransform, other.transform.position, hit))
{
enterPlayerPos = other.transform.position;
if (hit.collider.tag == "Player")
{
Debug.Log("its working now");
see = true;
Debug.Log(see);
}
}
}
}
function debug() {
Debug.DrawLine(myTransform, enterPlayerPos);
}
function OnTriggerExit (other : Collider) { if (other.gameObject.tag == "Player") { yield WaitForSeconds (5); see = false;
}
}
Regarding the main problem, if I have failed to address it, in the check wall function, it doesn't seem to be pushing the object back when it move in front of the wall. The rays show and are detecting the wall, but the object doesn't turn and isn't repelled.
Answer by Kryptos · Apr 04, 2012 at 07:14 AM
Try to use UnitySteer which is a port of OpenSteer into Unity.
http://forum.unity3d.com/threads/26217-UnitySteer-steering-library-for-Unity