Prevent enemy from falling off edge
Hi,
I have a (3d) game where the enemy charges at the player, but the player can quickly avoid the enemy and just let them fall off the platform they're on. I want the enemies to avoid edges, but I'm not sure how I should start. I'm using rigidbody.AddForce for the enemy movement so I can't just program them to stop since they keep on rolling.
Thanks
Answer by toddisarockstar · Feb 19, 2017 at 08:45 PM
rigidbody.velocity = Vector3.zero; will stop your enemys if you stop adding force!
have you considered adding walls around your platform and just turning of the renderers so the walls are invisible?
I just want the enemy to avoid edges, so I don't want to completely disable falling.
i'm not sure exactly what your levels / platforms look like. you could assign marker points in your platform/level so that your AI script had a way of knowing it was close to your edges. i have code for picking up location markers if you that would help you.
$$anonymous$$y platform is a square shape, so nothing complex... I also want my script to be compatible with other platforms, so that I can easily reuse them without needing to create scripts for each platform.
This Is a good universal script to know if items are within a designated box shaped area. i wrote it for 3d so it detects objects within x and z positions. it ignores up and down though that could be easily added. to use this code you simply make at least two empty game objects in your scene and name them "marker0" and "marker1". then move the markers to opposite corners of the box shaped area you would like to define. you can assign as many areas as you like as long as they are named in order. you only use two markers per area so if you want to add a second area, you would create "marker2" and "marker3" and so on. if your object is not within any area the script returns 100.
hopefully this is helpfull to know if you want to detect where your enemy objects are so you can make them turn around our whatever you want them to do.
it doesn't use any functions or colliders or physics its just simple math so it will work with any device/machine.
int i;
public Vector3[] markers;
bool b;
int rc;
Vector3 v;
public int marker;
void Start () {
b = false;
i = 0;
while (!b) {if (GameObject.Find ("marker" + i)) {i++;} else {b = true;}}
markers = new Vector3[i];
while (i>0) {i--;markers[i] = GameObject.Find ("marker"+i).transform.position;}
b = false;
i = 0;
}
// Update is called once per frame
void Update () {
v = transform.position;
marker = 100;
rc = 0;
while(rc!=200){i = 0;
if(markers[rc].x<markers[rc+1].x){
if(v.x>markers[rc].x){if(v.x<markers[rc+1].x){i++;}}}
else{if(v.x>markers[rc+1].x){if(v.x<markers[rc].x){i++;}}}
if(i==1){
if(markers[rc].z<markers[rc+1].z){
if(v.z>markers[rc].z){if(v.z<markers[rc+1].z){i++;}}}
else{if(v.z>markers[rc+1].z){if(v.z<markers[rc].z){i++;}}}
}
if(i==2){print(rc);marker=rc;rc=200;}
rc=rc+2;
if(rc>markers.Length-2){rc=200;}
}
}
Answer by Zee-Play · Feb 22, 2017 at 02:52 AM
From what I understood you want the enemies to stop before hitting the edge of your platform. Can just do a rayscan each frame like this and check if it collides with the platform, having the vertical rayscan always be done in front of your character (the direction he's moving)
If the rayscan returns null, then you stop all forces on the object and rigidbody.velocity = Vector3.zero like toddisarockstar said.