Hey I'm using Unity. I'm making a really sick car game, with dynamic car damage, crazy stunts, and wacky cartoonish graphics. I'm running into problems with math, though. Basically, I'm trying to make a function that inputs the damage done to the car and the Vector3 location of the impact. Then, the code should use this information to distort the mesh around the point of impact by a reasonable amount.
It looks like the following in pseudocode:
public void DestortMesh(collision point, amount of damage)
{
Radius = damage / 10;
for (each verex of the mesh)
{
if (the vertex is within the radius from the collision point)
{
distort the vertex
}
}
}
Here is the actual code:
void deform(float radius, ContactPoint contactPoint)
{
Debug.Log("Deform called with radius of " + radius);
Vector3 relativePosition = gameObject.transform.InverseTransformPoint(contactPoint.point);
for (int x = 0; x < vertices.Count; x ++)
{
Debug.Log(Vector3.Distance(vertices[x], relativePosition));
if (Vector3.Distance(vertices[x], relativePosition) <= radius)
{
//time to deform
vertices[x] += vertices[x] * Random.Range(-.1f,.1f);
}
}
mesh.SetVertices(vertices);
mesh.RecalculateBounds();
mesh.RecalculateNormals();
}
This looks all fine and dandy, right? but when I look at the distances between each vertex and the collision point (the debug log right before the if statement), they look like this:
basically, the output is weird. The speed of impact seems to affect it slightly, and for every impact, each vertex's distance is within a few 1000ths of each other. This makes it really hard to tell which points are close and which ones are far from the impact point. When trying out this in the game, the damage to the car is sporadic. For the most part, no damage is visible on the car. Then randomly, usually after a high speed impact is made, the car will become completely distorted, with every vertex changed.
You can see a video of this happening here:
So in the end, this is not the result I was aiming for. Something seems to be off with the Vector3 distance, but in the Unity forums all I'm getting are people saying there couldn't possibly be anything wrong with them. So I'm going to assume that there is something wrong with my code perhaps?
If you think you could help me, add me on Discord. Ramaraunt#6934