How i prevent 2 obejcts with kinematic checked in both rigidbodies collide?
I have this AI in enemies that goes to the player, i want to avoid that 2 objects collide (both of them are with kinematic checked in their rigidbodies). I need this because of the animations (they slide at the transition if kinematic is not checked) and i am using navmeshagent for the AI movement.
I went to Physics and disabled the layers collision but it not worked.
There is a workaround?
Answer by UnityCoach · Nov 05, 2018 at 03:01 PM
When moving a Kinematic Rigidbody, either through its transforms, or preferably with Rigidbody.MovePosition(), you need to check for collisions yourself, as Kinematic Rigidbodies will
not be applied any force (gravity, nor forces),
not suffer collisions, although other Rigidbodies will collide with them like they had an infinite mass.
You can simply use Physics.RayCast(), or better use Physics.BoxCast(), or Physics.SphereCast() to detect possible collisions on the way.
Something like this :
void FixedUpdate ()
{
// if nothing is in the way along 1 unit ahead
if (!Physics.RayCast (transform.position, transform.forward, 1f)
{
// move rigidbody 0.5f forward
rigidbody.MovePosition (rigidbody.position + transform.forward * 0.5f);
}
}
I'm using Nav$$anonymous$$esh to the movement. I made some tests and even with kinematic unchecked they are still colliding.
Nav$$anonymous$$esh Agents will ignore collisions.
It is often said that Navigation doesn't work with Physics. This isn't true, they just don't play nice with each other. You have to handle this agents/obstacles collisions.
You can use Obstacles. Agents will avoid them. If obstacles are set to Carve the Nav$$anonymous$$esh, it will allow agents to reroute to destination. Otherwise they will simply stop when stuck in a dead end.
You may also use GetPath(), and move the agent on the path yourself, checking for collisions using Physics.RayCast or BoxCast.
I see. But the real problem is that i'm spawning obejects as enemies, like an gropu of similar enemies. They have to go to a certain position to attack my player. But they collide with each other.
Answer by xainulabdeen · Nov 07, 2018 at 12:13 PM
Try to Change Collision Layers on Collision Matrix Edit > projectSettings > Physics
https://www.youtube.com/watch?v=S2m$$anonymous$$6$$anonymous$$Fdv0I see video till 5$$anonymous$$ts add Nev$$anonymous$$eshObstacle component to your agents and Check Property Carve so that it will make your objects static but moveable .
They will never overlap and find alternative path
Answer by tallesmaziero · Nov 07, 2018 at 12:41 PM
I made 2 videos to show what is happening. The video with 1 enemy is the way i want. The video with the 2 enemies is whats happening they keep pushing each other.