Unity limit physics walkable area to navmesh mesh - unity3d

I'm using physics to control my players and navmesh/agent for AI NPCs and occasional player movement. I'm wondering if there is a way to limit my walkable area for the player, when using physics to move, to the exact baked mesh from the navmesh so that the coverable ground is constant between the two.
Thanks in advance

One way could be to get the actually mesh in the navmesh and construct some sort of collision from it. You can get the actual mesh like this:
UnityEngine.AI.NavMeshTriangulation triangulatedNavMesh = UnityEngine.AI.NavMesh.CalculateTriangulation();
Mesh mesh = new Mesh();
mesh.name = "ExportedNavMesh";
mesh.vertices = triangulatedNavMesh.vertices;
mesh.triangles = triangulatedNavMesh.indices;
Then a recommendation for C# trigonometry calculations (like finding the convex hull and such) is NetTopologySuite

Related

Most efficient way of calculating big terrain collision mesh

I'm using Unity Engine to make a space exploration game. I have to implement the collision system for the planet I made with quadsphere and quadtree LOD, I thought of two ways:
Generate a BIG mesh collider for the hole planet and keep the collision activated only for the face that the player is in (the planet has 6 faces). This works fine because the mesh is created only one time, but the game keeps a mesh collider bigger than needed in the scene.
Use the quadtree LOD and create a new mesh collider every second, but just for the high resolution terrain near the player. The mesh collider used in this option is way less bigger than the first option, but is updated every second.
Which way is more approachable? And there are more efficient methods than these two?

Unity3D Collider Passing Through Other Colliders

The Problem: Mesh colliders on some rigidbody objects are passing through colliders on other objects.
Things I have tried:
With the assumption A is a GameObject with a RigidBody attached and B is a normal GameObject with a collider.
Give A a convex mesh collider
Give A a non-convex mesh collider
Give B a convex mesh collider
Give B a non-convex mesh collider
give B a box collider
give B a convex mesh and box collider
give B a non-convex mesh and box collider
Adjusting the weight of the rigidbody
I have tried all of these in all combinations of A and B.
In addition,
Colliders are not marked as triggers
All objects are on the default layer (0)
Checking isKinematic; doing this seemed to make gravity stop affecting the object, so I have left it as false.
Constraints: I want A to use a mesh collider since most of the objects involved are moderately complex, and fitting other colliders to them would take a while.
Weird Behaviour: I have some objects with both rigidbody and convex mesh collider where the collision is working fine with a non-convex mesh collider. This is inconsistent with other gameobjects. The objects have all of the same settings.
I am using unity version 2019.3.11f1 if that is relevant.
The object being used are from this package. Specifically, the filing cabinet with rigidbodies on the drawers works fine. The desk, office chair, pen, and open laptop all fall through the "floor" (a cube with all of the above colliders tested on it).
Do you have 'isKinematic' checked on the objects with rigidbodies that are going through other colliders? If so uncheck it so that external forces affect it.
edit you also need to click convex on the mesh colliders if they are colliding with other mesh colliders, Convex Mesh Colliders are limited to 255 triangles, are the objects that are not passing through have more than 255 triangles in geometry?
I am Assuming you have set your Collision Detection to Discrete In your gameObject's RigidBody, if so, please make sure to select Collision Detection to Continuous In your gameObject's RigidBody.
Reason Why this is not working
You are trying to use collision that has (speed > your Computer's frame speed) so, the frame is not catching the collider properly some time catches and some time fails to catch.
You are moving your GameObjects with tranform.translate or position etc. If so then make sure to use Rigidbody Related function for Positioning, rotating.
Solution Image For First Problem
I'm assuming
-you game object which contain the mesh component is very small ( bellow 0.7 size(x,y,z) so the game engine is little difficult to detect it)
and also
for the colliding objects at-least one rigid-body component must be attach to one object
This seems to be a common big problem, and I have been struggling with the same issue for days, and have finally fixed it, so am feeling like I should highlight one important thing:
move Rigidbody via MovePosition, not via just changing its position field directly, nor via changing position of GameObject holding it.
Changing position via "wrong" ways will "teleport" your rigidbody, physics won't fully work on it in that case.

Using a mesh collider in Unity with Bullet Physics

When using this ramp as a collider with Convex Hull the ball rolls on the edge like this:
Ramp and ball
Bullet physics settings
Blender
Thanks!
/T
Because that is not a convex shape.
Thanks! The creator of the plugin gave this solution to the shape:
approximate the shape with multiple simple colliders (boxes)
use BVHTriangleMeshShape if it is static
use the GImpactTriangleMeshShape if it is dynamic

Which side of my cube is hitting the plane below the cube

Im working in Unity and I have a simple scene. It consists of a cube which has a box collider on it. Below this cube is a plane
I want to know which side of the cube is hitting the plane at any given instant
One way to do this is to cast a ray from each side of the cube and determine which ray is colliding with the plane
But i fear that it may be performance heavy. Is there a way to do this in an efficient manner?
I used for same need:
1) Place plane for each side of cude;
2) Set clear color for planes;
3) Write script that check plane position (in your option need loop that search plane with lower position.
If you're just looking for a way other than just ray casting then you could create a method that takes in your cube's position and your plane's position and then do some calculations.
Vector3 heading = plane.transform.position - cube.transform.position;
float distance = heading.magnitude;
Vector3 direction = heading / distance;
From here you would just have to check what that direction is.

Unity: aligning object from multiple points to ground

I need to allow players to drag objects around the surface of my lowpoly ground so that the object sticks to the ground from certain points (in this case all the 6 legs). I tried Raycast to set position and rotation of the object with:
transform.position = hit.point;
transform.rotation = Quaternion.FromToRotation (Vector3.up, hit.normal);
This works quite nice but it always leaves some of the legs in the air because I set position and rotation of the whole transform not each leg individually. I could Raycast all of the legs individually but after that how can I position and rotate legs so that other parts of the model stays with the legs and the model doesn't "break".
Also I tried to give each leg a rigidbody and box collider but that just breaks the model by shooting the legs somewhere.
All ideas are welcome.