Get a specific edge on a mesh triangle

Hello ! I’m trying to figure out how to find the edge of a mesh triangle that’s closest to my character after he’s gone out of that mesh triangle.

So far I have this :

  private Vector3 GetTheDirection() {
    Mesh mesh = (groundHit.collider as MeshCollider).sharedMesh;

    Vector3[] vertices = mesh.vertices;
    int[] triangles = mesh.triangles;

    Vector3 p0 = vertices[triangles[groundHit.triangleIndex * 3 + 0]];
    Vector3 p1 = vertices[triangles[groundHit.triangleIndex * 3 + 1]];
    Vector3 p2 = vertices[triangles[groundHit.triangleIndex * 3 + 2]];
  }

But that gives me the 3 possible edges. How can I find out the one I’m looking for ?
See picture for illustration.

Thank you !

Look up “how to calculate distance to line segment.”

Once you have that code working, iterate your three lines and find the line that has the closest point of approach.

Ok I got the edge. But now I’m trying to make a normal out of it (I need it to push the player out in that direction).

So far I have this but it doesn’t seem to work all the time :

private Vector3 GetNormalToEdge() {
    Mesh mesh = (prevGroundHit.collider as MeshCollider).sharedMesh; // prevGroundHit is the hit from before I went out of the mesh.

    Vector3[] vertices = mesh.vertices;
    int[] triangles = mesh.triangles;

    Vector3 p0 = vertices[triangles[prevGroundHit.triangleIndex * 3]];
    Vector3 p1 = vertices[triangles[prevGroundHit.triangleIndex * 3 + 1]];
    Vector3 p2 = vertices[triangles[prevGroundHit.triangleIndex * 3 + 2]];

    Transform hitTransform = prevGroundHit.collider.transform;
    p0 = hitTransform.TransformPoint(p0);
    p1 = hitTransform.TransformPoint(p1);
    p2 = hitTransform.TransformPoint(p2);

    float dist0 = Vector3.Distance(p0, transform.position);
    float dist1 = Vector3.Distance(p1, transform.position);
    float dist2 = Vector3.Distance(p2, transform.position);

    float maxDist = Mathf.Max(dist0, dist1, dist2);

    List<Vector3> points = new() { p0, p1, p2 };

    if (maxDist == dist0) points.Remove(p0);
    else if (maxDist == dist1) points.Remove(p1);
    else if (maxDist == dist2) points.Remove(p2);

    Vector3 closestEdge = (points[1] - points[0]).normalized;
    Vector3 normalToEdge = Vector3.Cross(closestEdge, prevGroundHit.normal);

    return normalToEdge;
  }

I think it may be related to the orientation of the closestEdge vector ? Since it could maybe change sign if I’m closer to either of the 2 vertices it’s made from.

What do you think ?

Thanks for the help !

Oh so the way I’m getting my edge is actually wrong. As I can get in this case, where I’m actually registering the green edge instead of the wanted red one. So I’m not sure what to do here ?

I would recommend debugging and fixing your algorithm.

I think you have already seen this but it hasn’t changed:

Time to start debugging! Here is how you can begin your exciting new debugging adventures:

You must find a way to get the information you need in order to reason about what the problem is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the names of the GameObjects or Components involved?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer for iOS: https://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654

If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

If your problem is with OnCollision-type functions, print the name of what is passed in!

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

“When in doubt, print it out!™” - Kurt Dekker (and many others)

Note: the print() function is an alias for Debug.Log() provided by the MonoBehaviour class.

Well I know what the issue is, as I did debug it.

The issue is I don’t know how to get the edge I want from only having the information I have : the triangle, the vertices, the hit and my player.

How can I get the 2 vertices I want to form the edge I want ?

I can’t think of a way to do it. I can’t use the player velocity or position because it can be anything so I’m clueless.

Debugging won’t help. It’s not that I don’t know my algorithm doesn’t work as expected, it’s that I know why it doesn’t, but don’t know how to make it work.