Making Polygons Dissapear Dynamically when Damaged

So I’m working on a super crazy awesome car game, and the next thing I want to do, is make it so that when the car gets damaged, it dynamically removes polygons in the region where the car was hit. This is a crazy thing to code… so if you have any suggestions please say so!

Remember when changing these things that Meshes may be shared, so you’ll want to make sure it’s unique.

Furthermore, when chaning the size of the vertex array, the other related arrays resize… so you’ll want to copy out the vertices, uvs, normals, triangles, etc arrays. Modify to your needs. Then copy them back vertices first, than all the others.

What you maybe could do is have the car texture - mesh - sprite whatever it is, divided into lets say 20 parts, and then when that part gets it, it gets replaced with a damaged texture - mesh etc, that’s kinda what games like GTA do.

Alright, so a bit of a question:

I have good idea how to do this now. I have colliders all over the car to detect damage at different places. However, none of the colliders are working. Why is this?

using UnityEngine;
using System.Collections;

public class destructionDetection : MonoBehaviour {
    public destruction Master;

    void OnCollisionEnter(Collision collision)
    {
        Collider other = collision.collider;
        Debug.Log("Collision!");
        if (other.gameObject.tag == "Damager")
        {
            Master.doDamage(this.gameObject, other.gameObject, other.gameObject.transform.parent.transform.parent.gameObject.GetComponent<Rigidbody>().velocity.magnitude, other.gameObject.transform.parent.transform.parent.gameObject.GetComponent<Dot_Truck_Controller>().mass);
        }
        else
        {
            Master.doDamage(this.gameObject, other.gameObject, other.gameObject.transform.parent.transform.parent.GetComponent<Rigidbody>().velocity.magnitude, 0f);
        }
    }

}

You could just use collision.relativeVelocity and save half of that code :stuck_out_tongue:

oh wait a second
I have an idea

What if I do this:

I take the mesh, and split EVERY polygon into a seperate mesh, then on each polygon, I add a mesh collider. Then, I add a special script on each polygon. If it detects damage, it changes the shader of the polygon so it looks distorted and bent.

This shouldn’t be any more laggy than having a single mesh collider for the whole mesh i’m pretty sure.

Well technically, it will be more laggy because of the amount of colliders that would create, and if each of those polygons had a script attached to it the constantly looked for damage, you’d probably run into more lag.

Your best approach, suggest by @Stef_Morojna1 , would likely be to split the car into multiple parts and just replace each part when it is damaged, using variable damaged meshs. (i.e the right front bumper could be dented in leftward or bent outward or gone entirely, etc.)

Nah, I somehow want this to be dynamic. Ill keep thinking hmm.

EDIT: I’ll probably split it into large pieces instead of one mesh per polygon. But the problem is, Im confused how to make a shader that looks bent and damaged. Help?

Alright, so as you may or may not know, I made a custom rock generator with fancy shaders here:

I’m thinking, maybe I can use the same concept, to have the car’s mesh dynamically get damaged. The polygons would get bent at the point of impact. What do you think?

I’ve run into a serious bug I spent all day working on. I made a new thread, so this one stays on topic. Also, if my bug gets solved, it will be easier for other people who need it to find it.

Depending of how much realism you want, removing polygons from the car model does not seems to be a good way to simulate damages. You could artistically (in your 3D software) create damaged cars at different damage level, then swap the models at run time when damages occur. You could as well swap parts of the car instead of the whole model in order to make more localized or variable damages.

I’m aiming for more of a cartoonish result. So this is good enough.

I got it working! Here is a video of the damage. Note that the glass wont crack yet.

If you look at the debug log, there is a small bug I need to fix. Basically, the code works like this: every time damage occurs to the car, the damage is used to calculate a float radius. Then, if each point is within the radius from the contact point, the vertices are distorted

The problem is, there seems to be something a bit off. When the radius is less than one, nothing happens. But when a radius of 1 is hit, it all of a sudden damages the entire car in one hit. you can see this if you watch the video. I have no clue why this is happening, any help would be appreciated!

Got it! Releasing video soon. Its sickk