I Am trying to detect a collision point between 2 object, but when I do that seems the engine do some weird stuff.
this is my code:
public void CreateVisualPoint(Vector3 pos)
{
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = pos;
cube.transform.localScale = new Vector3(0.01f, 0.01f, 0.01f);
cube.GetComponent<MeshRenderer>().material.color = Color.red;
}
void OnCollisionEnter(Collision collision)
{
// Accedi al primo punto di contatto
ContactPoint contact = collision.contacts[0];
// Ottieni la posizione del punto di contatto
Vector3 contactPoint = contact.point;
// Log del punto di contatto
Debug.Log("Punto di contatto: " + contactPoint);
CreateVisualPoint(contactPoint);
}
how can you see here the cube generated is in mid air, without ANY SENSE!!
You seem to be using pretty small scales (0.001), and collision detection is not very precise at small scales by default. In the project settings, there is a Physics parameter called “Default Contact Offset”, which is set to 0.01 (m) by default. You could try reducing that, although the tooltip says that it should not be set too close to 0. The alternative would be to use scales closer to 1.
Unity uses PhysX, which is designed for real-time performance and not necessarily realism or high precision. It’s a trade-off between what looks good enough and what runs fast enough. Obviously, this will not work equally well in all situations. You can tweak the settings to get more precision, but there is always a cost. Unreal before UE 5 also used PhysX and now has its own “Chaos” physics engine, but I don’t know if either implementation is by default better than Unity’s (although I believe that Unity’s PhysX is a pretty old version, and the focus these days is more on Unity Physics / DOTS, which is a different thing altogether).