Answers for "Find UV coordinates of mesh without a raycast"
http://answers.unity.com/questions/1105729/find-uv-coordinates-of-mesh-without-a-raycast.html
The latest answers for the question "Find UV coordinates of mesh without a raycast"Answer by Bunny83
http://answers.unity.com/answers/1106003/view.html
Well, that's quite easy given your restrictions of 2d orthographic projection without rotation. The easiest way is to use barycentric coordinates. All you need [i have already posted over here][1]. Given that all your coordinates are within the same plane (just set z = 0 for all positions) you can simply use GetBarycentric() with the vertex positions of the two triangles and use InTriangle to determine in which triangle the given point is. Once you have the barycentric coordinates of your player and know the triangle you can calculate the uv coordinates by simply using
Vector2 uv = Vert1uv * bary.x + Vert2uv * bary.y +Vert3uv * bary.z;
Of course you need to use the same vertex order you used in GetBarycentric.
***edit***
Here's a 3d version of GetBarycentric:
public static Vector3 GetBarycentric(Vector3 aV1, Vector3 aV2, Vector3 aV3, Vector3 aP)
{
Vector3 a = aV2 - aV3, b = aV1 - aV3, c = aP - aV3;
float aLen = a.x * a.x + a.y * a.y + a.z * a.z;
float bLen = b.x * b.x + b.y * b.y + b.z * b.z;
float ab = a.x * b.x + a.y * b.y + a.z * b.z;
float ca = a.x * c.x + a.y * c.y + a.z * c.z;
float cb = b.x * c.x + b.y * c.y + b.z * c.z;
float d = aLen * bLen - ab * ab;
Vector3 B = new Vector3((aLen * cb - ab * ca), (bLen * ca - ab * cb)) /d;
B.z = 1.0f - B.x - B.y;
return B;
}
It's actually just a couble of dot products, however to save method / operator calls i've written those manually so it's only native float math except for a, b and c.
***second edit***
I just packed everything into a struct. That way it's more clear that barycentric coordinates are not some kind of cartesian coordinates. [I put it on the wiki][2]. The "in triangle check" is now included and can be tested with a simple property. It also has an Interpolate method which simplifies the usage a little bit. I added constructors for Vector2, Vector3 and Vector4 values as well as Color values. So you can feed a reference color and in determines the position in the triangle based on the given vertex colors. Also note Vector4 is treated like a four-dimensional vector and not like "w" normalized Vector3
[1]: http://answers.unity3d.com/answers/46816/view.html
[2]: http://wiki.unity3d.com/index.php/BarycentricTue, 01 Dec 2015 14:54:35 GMTBunny83