So basically, I found a bug in Vector3, and I know how to reproduce it. If I’m wrong, you can say “I told you so”. Eric seems to be certain there couldn’t possibly be a bug. I will fight you on this Eric
Alright lets begin the steps to reproduce…
Step 1: make an object.
Step 2: Add a collider, rigidbody, and this script to the object:
using UnityEngine;
using System.Collections;
public class reproductionsteps : MonoBehaviour
{
private Vector3[] vectors;
void Start()
{
vectors = gameObject.GetComponent<MeshFilter>().mesh.vertices;
}
void OnCollisionEnter(Collision collision)
{
for (int x = 0; x < vectors.Length; x++)
{
Debug.Log(Vector3.Distance(vectors[x], gameObject.transform.InverseTransformPoint(collision.contacts[0])));
}
}
}
Step 3: make sure the object has a decent sized mesh with many vertices.
Step 4: cause a collision, and look at the strange debug log results!
For starters this is not an issue with Vector3.Distance, your title implies that it is.
And the reason why the contactPoint and the vertice seem so far apart is because the contactPoint is in world space, and the vertices is in local space relative to the Mesh. It does not take into account any scaling, rotation, or translation of the mesh at all.
You need to transform those points into world space before comparing.
If you take the Transform of the GameObject that the Mesh is attached to, and you can call ‘Transform.TransformPoint’ on the vertices to get this world coordinate:
So its not the Vector3 thats the issue, its the contact point. But I figured it out, it was because it had to be in local coordinates and it was in global. Thanks though.
It’s not bugged. (I assume you mean Vector3.Distance, though the Vector3 class in general has no bugs either.) Did you even look at the code I posted? The code extracted from UnityEngine.dll that Unity is literally running? Nah, that was a rhetorical question, you obviously didn’t look at it. Here it is again:
public static float Distance (Vector3 a, Vector3 b)
{
Vector3 vector = new Vector3 (a.x - b.x, a.y - b.y, a.z - b.z);
return Mathf.Sqrt (vector.x * vector.x + vector.y * vector.y + vector.z * vector.z);
}
Kindly demonstrate any bugs in that code. You’re really going to claim that somehow Unity can’t manage the most basic possible math? You don’t want to “fight me” on this. (It’s not that Unity has no bugs, far from it, but they’re all edge cases of some kind. 100% of these “basic Unity math/logic bug!!!” topics have turned out to be user error, without fail, and this one is no different. If it was actually true, no Unity games would ever be able to run. Your example doesn’t show any bugs, but rather a lack of understanding of what you’re doing. Print out actual values for all steps, don’t make vague claims like “look at strange debug log results”.)
Also, please revert your edit and post a new message instead, since you’ve now made a big mess of this topic. Once you’ve done that, I’ll delete this message and re-post it as a proper response.