I have a bunch of AI actors chasing my player, and when they get within attack distance, they stop. However, when there are actors behind the stopped actors, they push the stopped actors closer.
If I use Is Kinematic, they phase through each other, this is not something I want.
So how can I make the stopped actors immovable to the other actors pushing shenanigans? I tried hard-setting the rigidbody velocity to Vector3.zero during the stopped state, but that didn’t work either.
AI collision avoidance by using physics collisions?
Hehe. Good luck with that!
Seriously though, making things immovable and letting them bump into each other is probably going to be a headache to tune and make stable with a realtime physics system. What you need here is some kind of avoidance system.
Have each enemy cast a ray along it’s velocity vector and if you hit another enemy then apply a force in the opposite direction to slow them down.
Oh my god I am retarded, thats brilliant. I actually wound up using OverlapSphere, it works like a charm. This is pretty much what I did:
function FixedUpdate(){
var neighbors : Collider[] = Physics.OverlapSphere(transform.position, 1.5);
for(var neighbor : Collider in neighbors){
if(neighbor.CompareTag("Enemy") || neighbor.CompareTag("Player")){
var neighborDir = (transform.position - neighbor.transform.position).normalized;
rigidbody.AddForce(neighborDir*25);
}
}
}