Hi Lukas

I got you fixed up bro, I couldn't resist the challenge of writing this and it works really well.

Code:

//variable defintions
var face_pos[3];//used to set position then distance
var face_ang[3];//used to subtract pan angles for turning ent
var face_trn[3];//limits the turning of the ent
//
//define definitions
define face_speed, 3;
//
define idle, 0;
define walk, 1;
var state;
//
define angDist, 25; // 25 degrees on trace angles
define checkN, 0; // normal tracing turn check
define checkR, 1; // collision tracing right turn check
define checkL, 2; // collision tracing left turn check
var turnState;
var dist1;
var dist2;
var check[3];
//
function testBall()
{
my.passable = on;
vec_scale(my.scale_x,0.10);
wait(1);
ent_remove(me);
}
//
function traceAngle(dist,ang_dist)
{
check.x = my.x + cos(my.pan+ang_dist) * dist;
check.y = my.y + sin(my.pan+ang_dist) * dist;
check.z = my.z;
//
ifdef dispTrace;
ent_create("sphere01.mdl",check.x,testBall);
endif;
//
return(c_trace(my.x,check.x,ignore_me));
}
//
action enemy {
while(!player) { wait(1); }
turnState = checkN; // start out normal checking and turning to the player
while(1) {
// normal checking first
if(turnState == checkN)
{
dist1 = traceAngle(100,-angDist); // right angle checking
dist2 = traceAngle(100,angDist); // left angle checking
}
// alter states if dist1 or dist2 returns > 0
if(dist1 > 0 && turnState == checkN) { turnState = checkR; }
if(dist2 > 0 && turnState == checkN) { turnState = checkL; }
// this is for normal turning to the player
if(turnState == checkN)
{
vec_diff(face_pos,my.x, player.x);
}
// dist1 returned a result above, set this state, now we turn to check.x
if(turnState == checkL)
{
vec_diff(face_pos,my.x,check.x);
dist1 = traceAngle(100,-angDist);
if(dist1 == 0) { turnState = checkN; } // back to normal state
}
// dist2 returned a result above, set this state, now we turn to check.x
if(turnState == checkR)
{
vec_diff(face_pos,my.x,check.x);
dist2 = traceAngle(100,angDist);
if(dist2 == 0) { turnState = checkN; } // back to normal state
}
// do the turning
vec_to_angle(face_ang,face_pos);
face_trn.pan = ang(face_ang.pan - my.pan);
if(face_trn.pan < 0) { my.pan += face_speed * time; }
else { my.pan -= face_speed * time; }
//
// your stuff here
//
wait(1);
}
}



I hope I explained everything ok in the comments, also the 100 passed as a parameter in traceAngle is the distance of tracing, so you can change that if you like. The code traces in 2 directions from the enemy and the angle is 25 degrees(which can also be changed in the define above)to the left and right of its x pos. When a distance is returned it sets the state and forces the turn vector(face_turn) to be the diffence of the new direction instead of the players position. Then the enemy turns to avoid the obstacle and once the trace is in the clear, its sets the state back to normal and begins tracing for new obstacles.

I know its a little backwards on the turning, you asked for a turn direction in the opposite, but I think this works pretty well.

Check out this demo to see it in action: Test Enemy Obstacle Avoidance

You'll see the enemy head towards the player to which you can't see, but watch how the enemy turns to go around the block in the middle.

I hope that helps you out bro.

-raiden


"It doesn't matter if we win or lose, it's how we make the game."
--------------------
Links: 3DGS for Dummies