I’ve got a little problem going on over here, and i can’t seem to find a solution. I’ve been making a simple Animal-AI for pigs, who are wandering around in the bounds of a fence, so far so good. When an animal get’s to close to the fence (detected via Raycast) it turns around 180 degrees and walks on. But the problem is: When two pigs walk into each other, one of them either starts spinning around in weird ways or flies up 30-50 metres in the air.
The pigs have a Rigidbody as well as a Caspule Collider (oriented in Z-Direction) attached to them, and they are getting moved by
Create a new layer called “Pig”
Set the Layer of the Pigs prefab to “Pig”.
Go to Edit > Project Settings > Physics
Make sure the checkbox in the Layer Collision Matrix is unchecked for pig-pig collision.
That will disable collisions for the pigs
Enjoy
I actually don't want the pigs to walk "through" each other. If two pigs in real world collide, one of them doesn't fly 50 metres up in the air. It can't be that hard to transfer this beahviour into unity
transform.Translate does a micro-teleport, as far as the physics engine is concerned. A little like Lancelot attacking in Monty Python’s Holy Grail. The “physics speed” (rigidbody.velocity) is left as mostly 0. You never get a real collision, caused by the physics engine moving them. Instead they just suddenly overlap, and it gets confused.
Physics wants to bounce the pigs away at their flipped speeds. Often both pigs have a tiny amount of down speed from gravity, so it bounces them both up, or velocity is close enough to 0 so bouncing does nothing, giving the vibrate.
I noticed you grab the controller, but then don’t use it:
If you instead move with controller.Move(Vector3.forward * Time.deltaTime * 1.9f); it should do the math for you. Translate and Move look similar, but Translate will gladly shove you through hills. Controller.Move does a lot of collision and ground slope detection to shove you around solid objects.
Alternately, lose the CharacterController and work with physics. Change the move line to:
rigidbody.velocity = Vector3.forward * 1.9f;
There’s no Time.deltaTime in there, since the units are meters/second. The physics engine will automatically apply T.DT each frame. You used rigidbody.AddForce in Start, but can’t use that here – it would thrust your pigs like rockets like a game of bumpercars.
You might get some vigorous, un-pig-like bounces. Playing with the physics material bounciness could help that.
I think the best way to prevent flying out is freezing contains in rigidbody. For example I freeze “z axel” and all things is ok. Unity physics is not ideal. Sorry for mistakes. English is not my native language
that would be really nice ;) that actually IS odd... i have tried locking all the rotation axes, but then the pigs start walking through each other, again resulting in one getting pushed up in the air... I'm close to dispairing...
I actually don't want the pigs to walk "through" each other. If two pigs in real world collide, one of them doesn't fly 50 metres up in the air. It can't be that hard to transfer this beahviour into unity
– remoteplayfreakhere's a little video clip i recorded, so you can see what's going on on my screen ;) http://www.youtube.com/watch?v=2DMRuoX4F2g
– remoteplayfreak