I’ve been reading through 3D Physics in the Unity documentation for a solution to a development problem.
I am trying to create a gun barrel that sits inside a turret mount. I would like the barrel to adhere to the physics of the mount, retain concave status so a bullet can pass through it, and be able to move back and forth as recoil and reset occur after firing the gun…
After reading the docs, this seems to amount to the title of the question; Concave mesh collider with dynamic rigidbody. However, this combination is not allowed…
Therefore, I am wondering how else I can achieve this result? The only kind of ideas I’m having is to make the barrel a combination of multiple convex meshes. This should achieve the concave nature of the barrel, without sacrificing the other physics properties. If there are any other obvious solutions however, I would be curious to hear them!
Can you have a convex barrel, and tell it to ignore collisions with the bullet, either using the collision matrix, or in code?
Apologies if this is too obvious a solution, or won’t work out for what you need, not sure where you’re at with physics:)
I tried that idea… It is still not working. The mesh isn’t detecting collisions properly. I’ve tried all the collision types, but they don’t work. I’ve tried all the combinations of static colliders and dynamic colliders… This really seems like the base physics systems is EXTREMELY archaic in application… I mean, what is it good for at this rate? Only primitive shape collisions? Why have a mesh collider in the first place then.
This is very disappointing. This kind of thing is why Unity gets a bad reputation. I’ve stood with Unity for so many years. But when something this simple doesn’t work correctly, it is hard to find optimism.
I only have a few things left to try. Increasing the physics system steps, or whipping up some kind of scripted kinematic/static collider approach… It will be very disappointing if these don’t work.
If I understood correctly what you want to achieve, there are two solutions I can think of:
use contact modification to allow certain regions of your collider to pass bullets; the API for this is not really intuitive, but it works fine if you understood the principle
There’s very good reasons why dynamic rigidbodies + concave meshes aren’t allowed: the combination is extremely brittle and extremely expensive.
It’s not cheap to calculate the intersection of arbitrary, possibly non-convex triangle soups: all individual triangles need to be tested for collision, and calculating a minimum escape vector is pretty much impossible so once an object is inside the collider due to tunneling, it cannot be pushed back out. However, there’s a simple solution for convex shapes (the GJK algorithm) that’s both robust and performant, so that’s why most realtime physics engines only support convex shapes for dynamic objects. Additionally, convex objects work well with continuous collision detection.
Convex meshes are not primitives. Only analytical shapes like boxes, capsules or spheres are.
MeshColliders are intended to be used for static level collision shapes. Things like walls, floors, large rocks, etc.
Tell me you’ve never used any other engine without telling me you’ve never used any other engine… Unreal doesn’t support this, Godot doesn’t either, GameMaker nope, can say the same for all engines I’ve ever worked with or heard about for that matter. Unity’s physics engine is Nvidia’s PhysX, which is probably the most widely used and battle-tested realtime physics engine in existence, along with Havok and Bullet. Not some janky / haphazardly put together system.
You’d be hard pressed to find any physics engine out there which supports efficient, robust dynamic concave meshes -if at all- the reason being there’s a simpler, cheaper, faster and overall better way to have dynamic concave shapes: multiple convex ones. This is the way most games do it, regardless of the engine they’re built upon.
I’ve never used any other engine. I’ve opened Unreal once, and I used GameMaker Studio for like three days… All Unity engine otherwise…
One thing I’ve considered is using the Animator for handling specific micro-adjustments of static objects. I will have to test it out some, but it handles complex shifts in object positions without scripts… It might work to fill in the gaps I have at the moment…
This is actually quite easy, you should not do concave collisions.
Use Capsule collider for barrel. (There is no cylinder collider unfortunately).
Barrel is separate rigidbody and mount is saparate, connected with configurablejoint.
When firing spawn projectile from the end of a barrel, also disable collision between barrel and fired projectile.
I will have to give this a shot… I am messing around with using the Animation of static objects now. But I suppose using a Capsule Collider like that would solve a lot of the problems.