- Home /
Procedural Object Avoidance
I have a "infinite" procedural terrain, how can I have AI that avoids gameobjects and walks around them to get to the target?
I can't use NavMesh, because it can't bake at runtime.
I am not sure if I can use A* because it seems to work with either predefined paths or a grid system which can't fit an infinite terrain. (I am not sure if it has some way to do this)
If you could give me any suggestions on what would work fine for my game(Minecraft type just without voxels)
Another question, my code looks pretty messy right now, could you help me simplify it? :)
your if statements where you don't actually do anythin except contain another if statement could be combined using the '&&' operator:
if(this){
if(that){
doSomething();
}
}
could be:
if(this && that){
doSomething();
}
And I don't understand what you are trying to achieve with lines 103-117, you seem to have way too many parenthesise '{}' as you close all your else statements twice (though i haven't actually counted) and you do the equivalent of 'if this whole list of things is true, set target to null, otherwise set target to null' so why bother with the if's at all!
The 103-117 all have a else statement, except the last one because if none of them are met then it should remove the target.
Answer by Scribe · Nov 16, 2014 at 01:38 PM
If you know you're obstacles can be avoided by going either way around them, as in they are single blocks of obstacleness, then you could have your enemy walk in your direction and if there becomes an obstacle a certain distance in front then move parallel until the enemy ca 'see' you again.
Alternatively, I think the best bet would be to update you navigation grid proceduaral as you update your terrain or whatever!
Yea, that is what I have tried to do, with a last known position, it will then walk to that position and check if the player is there, the problem is I want the AI to go to a specific position, and avoiding trees and houses etc.
here's a very simple example code of what I mean:
public Transform target;
public string obstacleTag = "obstacle";
public float dist = 5;
public float speed = 1;
Vector3 dir;
void Update () {
dir = (target.position - transform.position).normalized;
dir.y = 0;
RaycastHit hit;
if(Physics.SphereCast(transform.position, 0.5f, dir, out hit, dist)){
Debug.Log(hit.collider.tag);
if(hit.collider.tag == obstacleTag){
transform.position += (speed*new Vector3(dir.z, 0, -dir.x)*Time.deltaTime);
}else{
transform.position += (speed*dir*Time.deltaTime);
}
}else{
transform.position += (speed*dir*Time.deltaTime);
}
}
so you have your enemies target transform which he moves towars while he can, and if he raycasts and hits something with tag "obstacle" he will side step for a while until he can see the object again. Note that I have not handled ignoring collisions with your self in this example so when I was testing I simply deleted my enemies collider, you should set up layer masks for this.
@Scribe This still makes the problem, it should be able to move through a maze, if the player was teleported to the other end.
If you need it to go through a maze you will have to have a grid based system, just update the grid when you update your terrain mesh.