Search Unity

Vehicle destruction for demolition derby

Discussion in 'Physics' started by TurnkeyAgenda24, Jan 12, 2022.

  1. TurnkeyAgenda24

    TurnkeyAgenda24

    Joined:
    Aug 20, 2021
    Posts:
    27
    Hello, I was wondering how to make the vehicles that I use have mesh deformation and destruction in a demolition derby-style game. are there any basic free scripts or anything that could help?
     
  2. Geckoo

    Geckoo

    Joined:
    Dec 7, 2014
    Posts:
    144
    Hello. It reminds me a fantastic game on PS1 when I was a young player - DestructionDerby :) Obviously, you are a newbie using Unity. This is not a reproach - we were newbies too. Your question is really interesting and concerns mesh deformation in real-time. You should take a look at this page, but it's not for newbie. Good reading ++

    https://catlikecoding.com/unity/tutorials/mesh-deformation/

    Also, there is a relevant video about deformation process. All is explained :

     
    Last edited: Jan 12, 2022
  3. Geckoo

    Geckoo

    Joined:
    Dec 7, 2014
    Posts:
    144
    I share with you a code which works as expected, but you have to improve it in order to get a better result. It deforms a mesh in real-time, checking a radius and a minimum impact magnitude. You can define malleability of the object. I keep the same principles which have been explained in the video which I shared with you above. I assume that it could be useful in order to start your project. I tested it on a sphere with a mesh collider and it works ++

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class DestructionDerby : MonoBehaviour
    7. {
    8.     public float minImpulse = 1f;
    9.     public float strength = 0.1f;
    10.     public float radius = 0.2f;
    11.  
    12.     private Mesh m;
    13.     private MeshCollider mc;
    14.     private Vector3[] verts;
    15.     private Vector3[] iVerts;
    16.  
    17.     private void Start()
    18.     {
    19.         m = GetComponent<MeshFilter>().mesh;
    20.         mc = GetComponent<MeshCollider>();
    21.         iVerts = m.vertices;
    22.     }
    23.  
    24.     private void OnCollisionEnter(Collision collision)
    25.     {
    26.         float impulse = collision.impulse.magnitude;
    27.         // limit to detect collision
    28.         if (impulse < minImpulse)
    29.             return;
    30.         // all vertices in mesh
    31.         verts = m.vertices;
    32.         // contact point & normal
    33.         Vector3 point = transform.InverseTransformPoint(collision.GetContact(0).point);
    34.         Vector3 normal = transform.InverseTransformDirection(collision.GetContact(0).normal);
    35.         // deformation according radius
    36.         for (int i = 0; i < verts.Length; i++)
    37.         {
    38.             float scale = Mathf.Clamp(radius - (point - verts[i]).magnitude, 0, radius);
    39.             verts[i] += normal * impulse * strength * scale; // modify vertices pos
    40.         }
    41.         // recalculate mesh
    42.         m.vertices = verts;
    43.         mc.sharedMesh = m;
    44.         m.RecalculateNormals();
    45.         m.RecalculateBounds();
    46.     }
    47.     // clean up to previous mesh
    48.     private void OnApplicationQuit()
    49.     {
    50.         m.vertices = iVerts;
    51.     }
    52. }
     
    Last edited: Jan 12, 2022
  4. TurnkeyAgenda24

    TurnkeyAgenda24

    Joined:
    Aug 20, 2021
    Posts:
    27
    thank you :) I have played a few demolition derby games like wreckfest and the older demolition racer 2.