Unity - Car Crash Physics With Dynamic Mesh Deformation [C#]

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ม.ค. 2025

ความคิดเห็น •

  • @polysyncproductions6199
    @polysyncproductions6199  4 ปีที่แล้ว +10

    Timestamps:
    0:00 Intro
    0:57 Scene & mesh setup
    2:27 The code 😱
    4:50 Parameters
    5:22 Testing
    6:20 Outro

  • @elliotfriesen6820
    @elliotfriesen6820 4 ปีที่แล้ว +8

    you don't understand how amazing you are

  • @dererzherzog
    @dererzherzog 4 ปีที่แล้ว +16

    To be able to explain such a complex concept in less than 7 minutes is a great gift. Be sure to remeber that, son!! ;) Great work! 👍
    Those missing Likes and Subs will come in due course...

  • @yoshiro3549
    @yoshiro3549 3 ปีที่แล้ว +4

    idk why but my car just don't seemed to deform, it made in 3dsmax, also since my car has more than one part, when i play in unity with mesh collider and ridgidbody the model just fall apart

    • @polysyncproductions6199
      @polysyncproductions6199  3 ปีที่แล้ว +1

      This is because you need to add deform, convex mesh collider, and a rigidbody to every object you want to deform. Also check the rigidbody isKinematic to true.

  • @jeffubeku6143
    @jeffubeku6143 2 ปีที่แล้ว +1

    This is one of my favorite TH-cam channels. He knows the stuff and spills knowledge. You should do more tutorials

    • @jeffubeku6143
      @jeffubeku6143 2 ปีที่แล้ว

      Between I’ve added you on discord. It will be an honor if you will oblige to mentor me even if I have to pay.

  • @MohammadAlizade
    @MohammadAlizade 4 ปีที่แล้ว +7

    wait enable read/write will apply the deformation to the original file?

    • @polysyncproductions6199
      @polysyncproductions6199  4 ปีที่แล้ว +5

      Good question. Read/write has to be enabled because we have to read every vertex in the mesh, store it, and make a new mesh instance that is deformed. Since I'm using filter.mesh rather than filter.sharedMesh, it creates a new mesh instance for each individual object instead of changing the file.
      Here's a little snippet from the unity docs:
      "When you enable (read/write), Unity uploads the Mesh data to GPU-addressable memory, but also keeps it in CPU-addressable memory. This means that Unity can access the Mesh data at run time, and you can access it from your scripts. For example, you might want to do this if you’re generating a Mesh procedurally, *or if you want to copy some data from a Mesh.*"

    • @MohammadAlizade
      @MohammadAlizade 4 ปีที่แล้ว +3

      @@polysyncproductions6199 Oh thanks for the details, i looked up the docs and started from scratch.

    • @polysyncproductions6199
      @polysyncproductions6199  4 ปีที่แล้ว +2

      @@MohammadAlizade Good luck with it!

    • @MohammadAlizade
      @MohammadAlizade 4 ปีที่แล้ว +2

      @@polysyncproductions6199 i managed to make one based on your code and another guy, also added an array of meshfilters for body parts like doors, bumpers it works great on mobile on a 10k geometry.

    • @polysyncproductions6199
      @polysyncproductions6199  4 ปีที่แล้ว +3

      Sounds awesome!

  • @EngiNetion
    @EngiNetion 4 ปีที่แล้ว +2

    very underrated

  • @thespontaneoustomato2676
    @thespontaneoustomato2676 4 ปีที่แล้ว +10

    You are so underrated! U deserve way way way more subs! And I'm not just saying that I just subbed right now! I can't believe I didn't find this channel earlier!!!

    • @polysyncproductions6199
      @polysyncproductions6199  4 ปีที่แล้ว +3

      Thank you so much! You have no idea how much that means to me. I'm currently on a trip, but when I get back I have a couple more tutorials I want to get done, so stay tuned! :)

    • @thespontaneoustomato2676
      @thespontaneoustomato2676 4 ปีที่แล้ว +1

      @@polysyncproductions6199 k :)

    • @thespontaneoustomato2676
      @thespontaneoustomato2676 4 ปีที่แล้ว +1

      @@polysyncproductions6199 do you have a discord server that we can join and chat?
      (By we I mean all your fans)

    • @polysyncproductions6199
      @polysyncproductions6199  4 ปีที่แล้ว +1

      @@thespontaneoustomato2676 I do, its still WIP but you are welcome to join. discord.gg/KGmEc3U

  • @LemonMontage420
    @LemonMontage420 3 ปีที่แล้ว +1

    Something you can add to it is get the hit normal of the collision, so that the deformation happens in the direction of the hit.

  • @TheHelmetMusic369
    @TheHelmetMusic369 ปีที่แล้ว

    My bro 🔥🔥🔥🔥🔥🔥🔥🔥

  • @hajjex_9086
    @hajjex_9086 3 ปีที่แล้ว +2

    Can we make it like also bend from the rear end of the wall?
    So that it makes.more sense.
    Thanks

  • @CaptainSeanTV
    @CaptainSeanTV 3 ปีที่แล้ว +1

    This is amazing! Thank you for making this! U are amazing!

  • @leohunter5609
    @leohunter5609 3 ปีที่แล้ว

    THAT'S A LOT OF DAMAGE! LOL! Great job bro. This was really informative and it really helps. Thanks!

  • @Hurkaaa
    @Hurkaaa 4 ปีที่แล้ว +2

    hi! How can I fix this problem? The collider is fixed in the positon but the mesh is moving back (sry for bad eng :D)

    • @polysyncproductions6199
      @polysyncproductions6199  4 ปีที่แล้ว +1

      Disable convex mesh and enable isKinematic on the rigidbody.

    • @Hurkaaa
      @Hurkaaa 4 ปีที่แล้ว +1

      @@polysyncproductions6199 Same :/

    • @polysyncproductions6199
      @polysyncproductions6199  4 ปีที่แล้ว +1

      @@Hurkaaa Are the rigidbody and collider together? Not separate objects?

    • @Hurkaaa
      @Hurkaaa 4 ปีที่แล้ว +1

      @@polysyncproductions6199 Yes the rigidbody and the collider is together.

  • @Benign1
    @Benign1 3 ปีที่แล้ว +2

    I am the 100th subscriber yay!

  • @SergSamixonGD
    @SergSamixonGD ปีที่แล้ว

    Cool job man!

  • @K3rhos
    @K3rhos 4 หลายเดือนก่อน

    For some reason, this is not working entirely, the collider mesh get properly deformed, but the visual (the mesh filter) doesn't change at all ! Any reason to that ?
    Read/Write is enable btw, the mesh filter is also properly assigned.
    Edit: Oh okay fixed it, this is because I have multiples mesh filter on my car, and the script was actually using the wrong one !

  • @Berkonomi
    @Berkonomi 4 ปีที่แล้ว +3

    woooowww soo gooodddd

  • @nuclearapples1412
    @nuclearapples1412 3 ปีที่แล้ว +1

    When i collide with a wall or the car the mesh moves but the world space position of the object is the same like it's never moved? it seems like the entire models verts are being shifted

  • @bdenix1997
    @bdenix1997 3 ปีที่แล้ว +1

    great tutorial btw. loved it.
    i was trying to raycast from camera to target mesh and find the vertices that got hit from triangle index
    which i could deform only those 3 vertices and looked very sharp.
    i did that because i thought looping through all the vertices would be slow. but trying your code, it wasn't. :D and its better. looks nice, works nice. great.

    • @polysyncproductions6199
      @polysyncproductions6199  3 ปีที่แล้ว +1

      That's a great idea. Raycasts are fun to play around with.
      Glad you liked the tutorial. :)

  • @BrianBakerCA
    @BrianBakerCA 4 ปีที่แล้ว +4

    Really great tutorial.
    I thought resetting the mesh would be as simple as setting it back to the old verts. Any idea why this isn't working?

    • @polysyncproductions6199
      @polysyncproductions6199  4 ปีที่แล้ว +1

      I would need to see your code, but just remember to make sure that you are setting the filter mesh's vertices instead of the mesh file itself.
      Ps. I'm glad you liked the tutorial :)

    • @BrianBakerCA
      @BrianBakerCA 4 ปีที่แล้ว +1

      @@polysyncproductions6199 Yeah it turns out you're actually changing the default mesh if you don't clone it first. Got it working now! Thanks again

    • @polysyncproductions6199
      @polysyncproductions6199  4 ปีที่แล้ว +1

      @@BrianBakerCA Happy I could help! Good luck on your project. :)

    • @nuclearapples1412
      @nuclearapples1412 3 ปีที่แล้ว +1

      PolySync Productions if i i call a void say something like "ResetMesh()", would i just set filter.mesh.vertices = originalVerticies; ?

    • @polysyncproductions6199
      @polysyncproductions6199  3 ปีที่แล้ว +1

      That should work. The easiest way would probably be to just set the mesh instead of the vertices. So:
      filter.mesh = defaultMesh; where defaultMesh is a public variable of whatever mesh you are using.

  • @leonardojensen2654
    @leonardojensen2654 4 ปีที่แล้ว +1

    Great video you earned a sub from me, hope to see more videos in the near distance future.

  • @hajjex_9086
    @hajjex_9086 3 ปีที่แล้ว +2

    Thanks man

  • @StreamSport-o9h
    @StreamSport-o9h ปีที่แล้ว

    can i get the script boys the description link isn't working

  • @Sultan___
    @Sultan___ 3 ปีที่แล้ว

    Non-convex MeshCollider with non-kinematic Rigidbody is no longer supported since Unity 5.
    i get a error

    • @polysyncproductions6199
      @polysyncproductions6199  3 ปีที่แล้ว

      Select the object that you put the script on, and turn on isConvex on the mesh collider component

  • @hussainfallah5196
    @hussainfallah5196 2 ปีที่แล้ว

    I think that the collider didn't change even after changing the mesh so is there a solution for that and thanks for the tutorial

  • @rocketshader2699
    @rocketshader2699 2 ปีที่แล้ว

    This tutorial is amazing! Thank you! I think models should be in a modular fashion if you wanna make a deformation based game to keep number of searched vertices minimum.

    • @polysyncproductions6199
      @polysyncproductions6199  2 ปีที่แล้ว

      Glad it was useful! And yes, I would recommend splitting up models into different parts to keep it easy on the system.

  • @runnair
    @runnair 3 ปีที่แล้ว

    Very useful tutorial!

  • @arsylgames
    @arsylgames 4 ปีที่แล้ว +4

    This is a really good video and I like the car damage a lot! It would be cool if you would make a dynamic car damage tutorial but I can uderstand it is hard to make it.

    • @polysyncproductions6199
      @polysyncproductions6199  4 ปีที่แล้ว +2

      I'm glad you liked the video!
      Are you talking about a more in-depth tutorial on making extensive car damage such as breaking windows and doors that fall off? That would definitely make a good addition, I'll have to think about it. :)

    • @arsylgames
      @arsylgames 4 ปีที่แล้ว +4

      PolySync Productions breaking glass and falling doors. Than you have a really good car damage system

  • @elliotfriesen6820
    @elliotfriesen6820 4 ปีที่แล้ว +1

    Do you have a updated discord link?

  • @ahsanali7258
    @ahsanali7258 2 ปีที่แล้ว

    Thank you man

  • @ryanoneill1614
    @ryanoneill1614 4 ปีที่แล้ว +2

    Hey great video! Hope your channel takes off. Does this perform okay on mobile platforms?

    • @polysyncproductions6199
      @polysyncproductions6199  4 ปีที่แล้ว

      Hey thanks man! The script is pretty CPU intensive since I didn't optimize it for GPU, but it should be okay for a limited number of low poly models.

  • @54egg
    @54egg 2 ปีที่แล้ว

    Real nice video. I realize post is 2 years ago -- Just getting started with Unity and C# so your code it quite helpful. One thing I noticed is your calc. of pointPosition on line 52 could be moved up a few lines outside of mesh vertex loop so it is done only once and not for every vertex since it is "loop invariant", not sure if the c# compiler would be smart enough to figure that out. Also, wondering if allocation of deform Vector3 should be done once outside all loops?? Again, maybe c# compiler would recognize what was going on with allocation request followed by one reference...

    • @polysyncproductions6199
      @polysyncproductions6199  2 ปีที่แล้ว

      Thanks for the pointers, my code definitely wasn't perfect but it got the job done haha. Hindsight, doing it on the GPU with a compute shader would be much more efficient.

  • @HideOnDark
    @HideOnDark 2 ปีที่แล้ว

    links are not working

  • @vinobalan.official
    @vinobalan.official 3 ปีที่แล้ว +1

    how to make Mesh Deformation like this super smooth (@ )

  • @trgamer3193
    @trgamer3193 4 ปีที่แล้ว +1

    Great job ! but Script link is not working. Can you reupload real quick?

    • @polysyncproductions6199
      @polysyncproductions6199  4 ปีที่แล้ว +1

      Thanks!
      Does this link work for you?
      pastebin.com/GZniJb4H

    • @juniordeveloper7989
      @juniordeveloper7989 2 ปีที่แล้ว

      @@polysyncproductions6199 no

    • @polysyncproductions6199
      @polysyncproductions6199  2 ปีที่แล้ว +1

      @@juniordeveloper7989 docs.google.com/document/d/1XIcXxaqd8gTMear454FsZOgrVUcHm-_O-CEfESu_k80/edit?usp=sharing

  • @Acharad67
    @Acharad67 3 ปีที่แล้ว

    I think the script links are broken, can you share a new link?

  • @jayantbarthwal4470
    @jayantbarthwal4470 4 ปีที่แล้ว +2

    will it work for android

    • @polysyncproductions6199
      @polysyncproductions6199  4 ปีที่แล้ว +2

      Yes, it will work, but you need to make sure the models are very simple or it will lag. I would remove the line of code that changes the mesh collider because that causes the most lag. :)

  • @valzay4888
    @valzay4888 3 ปีที่แล้ว

    Thank you

  • @juniordeveloper7989
    @juniordeveloper7989 2 ปีที่แล้ว

    can't find the code

    • @polysyncproductions6199
      @polysyncproductions6199  2 ปีที่แล้ว

      docs.google.com/document/d/1XIcXxaqd8gTMear454FsZOgrVUcHm-_O-CEfESu_k80/edit?usp=sharing

  • @raveltammeleht6278
    @raveltammeleht6278 4 ปีที่แล้ว

    very interesting, have you tried adding bends to the damage? Like in Driver, when you collide, the hoods middle part pops up a bit. I guess it's just some so sort of average between the collision and the center of mass, causing some sort of a movement??? Not sure.. what do you think?

    • @polysyncproductions6199
      @polysyncproductions6199  4 ปีที่แล้ว

      Interesting idea. If you split the car into many different parts (example one part for the hood,) you could make a blend tree that would blend from 0 = no damage, to 1 = fully bent hood. Depending on how hard the collision is for each part you can interpolate between normal and damaged hood, instead of going through the trouble of making very complicated cripple physics. That's how I would implement that.

    • @raveltammeleht6278
      @raveltammeleht6278 4 ปีที่แล้ว +1

      @@polysyncproductions6199 hmm, interesting idea, haven't thought this way before. Here, you can try out my generic physics for the next video, trying out that? :) www.dropbox.com/s/1gnh2ddf33ousr6/pro3dmodels_GVP05.unitypackage?dl=0

  • @harshvardhansen7226
    @harshvardhansen7226 3 ปีที่แล้ว +3

    Bro, you have a 106 subs but your video is showing ads

    • @LemonMontage420
      @LemonMontage420 3 ปีที่แล้ว +4

      It's because TH-cam in their infinite wisdom decided to start showing ads on all videos, but you need to be monetized in order to get money from those ads

  • @w0nnafight
    @w0nnafight 3 ปีที่แล้ว +1

    Why did you stop uploading? Btw you look like Christina Hendricks

  • @Berkonomi
    @Berkonomi 4 ปีที่แล้ว +1

    mesh deform
    we want training videos

  • @ziad_vfx
    @ziad_vfx 3 ปีที่แล้ว +1

    please make arbic translate your videos are very nice