Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Procedural snake mesh and animation

Discussion in 'Scripting' started by flytraparts, Jul 18, 2021.

  1. flytraparts

    flytraparts

    Joined:
    Dec 2, 2019
    Posts:
    11
    Hi there, i hope i'm in the correct category

    i always wanted to know how to achieve a procedurally created snake
    something like this

    or this


    i have experience with procedural mesh generation using splines and geometry data for road generation and also some experience with procedural animation using IK

    but i couldn't wrap my head around how to make a snake mesh procedurally while having it animated
    and getting it to grow in length

    i hope you can guide me to the right direction
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Generally you're going to keep a series of points in space where the centers of the snake are, and how they move along.

    When they grow in length, you add more of those points, just like your basic SNAKE game as seen in the 1976 "Blockade" game, which started snake.

    BUT... what your procedural mesh code will do is regenerate the nice smoothly-moving outer shell of the snake, based on the positions of that series of points.

    The smoother and curvier it is the more you will have to contend with subdividing the mesh adequately so that the surface of the snake does not appear to shimmer in terms of its thickness as the segments of the body pass a given point in time.
     
    flytraparts likes this.
  3. flytraparts

    flytraparts

    Joined:
    Dec 2, 2019
    Posts:
    11
    Thank you very much for you input
    I thought about that but wouldn't that cause performance issues when you keep reapplying the vertices uvs etc.. to the mesh ?
    like each time you move you regenerate the mesh from scratch or is there a way to make the vertices follow the points, just like bones when animation ?
    maybe i'm just overthinking things
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Sounds like speculative performance worry to me. If your snake is 200 polygons, probably not gonna be an issue. If your snake is 200,000,000 polygons, yeah, it will probably leave a mark trying to update it each frame.

    Sure, you could use bones I guess. Somethings gonna have to stretch though... I mean you can speculate about 27,000 different technologies, but it is always better to roll some code and build and run it on the actual target hardware.

    If you have performance issues, attach the profiler (Window -> Analysis -> Profiler). If you suspect that you will have issues, attach it earlier and oftener and at every time you change what the computational load might be.

    This is just the economics of engineering against real world hardware.
     
    flytraparts likes this.
  5. flytraparts

    flytraparts

    Joined:
    Dec 2, 2019
    Posts:
    11
    Thank you, i'll do that and worry about performance when it actually causes them