Search Unity

Question What's everything I need to know to program a snake-like creature?

Discussion in 'Scripting' started by SoundStormLabs, Jun 5, 2023.

  1. SoundStormLabs

    SoundStormLabs

    Joined:
    May 6, 2017
    Posts:
    187
    It's a vague question, but I'm starting to learn C# and I'm realizing most tutorials are for humanoid creatures. But, something I want to try out is implementing a snake-like creature. If I'm just starting out, what are things I need to make sure to note for constructing either NCP or controlled script for a creature that moves in this manner?
     
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,116
    Starting out means doing exactly what the most popular tutorials do, and learning the very basic of things. If they don't do snakes, there is quite possibly a good reason for that.

    Your question has a thousand answers, none of which is at your current level of understanding. The easiest one, however, would be to simply learn how to animate a snake, and then apply this animation in Unity. You don't need Unity to make such an animation though.

    If you were hoping for "well click here and there" boy I wish such high concepts were easy to make anywhere.

    Natural snake motion has many many invisible physical constraints that make it look natural. To make any kind of fluid procedural movement is at the very top of mastery of animation, physics, and procedural generation.

    However you could go after a hybrid approach with classical animation and blend trees, but you would still need to learn these areas well, before you could try it with a snake.
     
    Last edited: Jun 5, 2023
    Kurt-Dekker likes this.
  3. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,116
    A very advanced answer to your question is "by implementing a physics solver for rope-like behavior that can collide with the environment but avoid self-collisions". And I don't know that many people who can pull this off. It's even beyond me, and I'm occasionally tipping my toes in rope physics and crazy stuff you won't easily find in a Youtube tutorial.

    That said, I've seen a couple assets on the asset store that can do this in a convincing manner.

    To fine tune the answers you're getting, you should probably try to rephrase your question into something more specific, and hopefully more down to earth, because this is a wild topic. Try by showing what kind of snake you had in mind. Depending on your project, there might be some tricks that can be employed, given that the snake motion can also be approximated.
     
  4. SoundStormLabs

    SoundStormLabs

    Joined:
    May 6, 2017
    Posts:
    187
    I'm familiar with animation in other software, it doesn't need to be the most advanced in the world, but I guess I'm wondering how that works in Unity. Can I import a splike IK rig from Blender to Unity, or do I somehow have to call inverse kinematics along a curve the armature is parented to?
     
  5. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,116
    Well Unity does support procedural IK rigs, but I'm not sure if it's compatible with Blender (rigs are okay, but I'm not sure what else there is to it, you need to make constraints all over again). I've seen it used only in the context of legged monstrosities, because with IK you kind of really care about how many joints there are. With snakes, you have just one spine and far too many joints, I'm not sure if that package was made with that in mind.

    But if you think about it, legs work completely differently when it comes to ground contact (which is mostly why IK systems are used, for walking animations; well, that and Euphoria-like dynamic grappling, if you remember that tech from GTA), because you just track the feet and shoot rays to find contacts. How would that work with a snake's body? That's too many rays, so it has to be really a full-on collider contact, and that smells to me like a proper simulation.

    I mean it's doable, but I bet you'd have to roll up the sleeves.
     
    Last edited: Jun 5, 2023
  6. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,116
    This is not an advertisement, just to illustrate what I'd consider a properly simulated snake.
    It's a paid asset though.


    (Btw I don't have a fear of snakes, and I find this mesmerizing. My wife would probably die. It's better not to show this to her.)
     
    adamgolden, Bunny83 and Kurt-Dekker like this.
  7. icauroboros

    icauroboros

    Joined:
    Apr 30, 2021
    Posts:
    168
    orionsyndrome likes this.
  8. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,116
    icauroboros likes this.
  9. SoundStormLabs

    SoundStormLabs

    Joined:
    May 6, 2017
    Posts:
    187
    Let's say I'm interested in making a simple snake, not necessarily as complex as what you've shown so far.

    What do I look up to rig and animate such a creature so that it is still controllable by a player, with basic movements though still "flexible" movements? Is Unity set up to rig characters? Is this normally done in another program. I'd appreciate some guidance. Or, if they can't be flexible through a bezier curve, perhaps I can try to experiment with a workaround, adding more typical bones to the armature.
     
  10. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,116
    Rigging is best done in Blender and imported into Unity. Though I'm not an expert really.

    After importing, you can install the Animation Rigging package icauroboros linked, via Package Manager.
    You'll find documentation on that link.

    Then you find a video explaining how to work with it. There are good videos on this topic.

    If you don't want to experiment with IK, then you likely want to learn how Animator works, you basically import the keyframe animations from Blender, and you can then blend them through some controller input. Snake rig should work, just make sure you don't use a humanoid rig.
     
  11. SoundStormLabs

    SoundStormLabs

    Joined:
    May 6, 2017
    Posts:
    187
    I've created an object and rig in blender. I successfully dragged and dropped the blender file into my Unity scene.

    Now what? I've looked at several different youtube videos and none of them really explain the process. There's one that uses playmaker but is one of the most god-awefully anti-user-friendly videos I've ever seen, speeding through every single detail not even bothering to explain what playmaker even is.

    THEN there's other videos, but all of them have to do with blocky creatures comprised of multiple disjoint cubes or spheres, which isn't what I'm going for.

    So, it looks like there's various ways to set up the player control movements, but how the bones animate and respond and curve in a 2D plane with player controls seems to be a mystery that has evaded even the most experienced professionals, unless someone with videography and teaching experience has managed to upload a user friendly video for this purpose.
     
  12. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    That's great but everything in a computer is sampled at some interval. EVERYTHING.

    If you use an animator to skin the snake, there will be a specific number of bones constituting the armature.

    You will manipulate those bones according to whatever snake physics you want to attain.

    The only point of the animator / bone / rigging system is to help visualize it.

    It absolutely WILL NOT handle the physics of what you contemplate.

    I haven't seen even one game that truly does snake physics.

    Even the links in that ObiRope are obviously just dragging it around by the head as if the head was held aloft by a pair of barbecue tongs.

    That's not how a snake works. Snakes are not magically dragged through the air by their head.

    Snakes are subject to the real world of physics and as such they could perhaps be modeled an inverse kinematic chain from where the weight lies on the ground going up to any bones leaving the ground.

    Go nuts, impress us!!
     
  13. SoundStormLabs

    SoundStormLabs

    Joined:
    May 6, 2017
    Posts:
    187
    I'm fine with that, I'd prefer the snake head to control the movement.

    This is good if true, now I know rigging in Blender isn't a complete and utter waste of time.

    So it looks like there needs to be some way to apply the idea of cubic, discrete snake physics, to bones of an armature, which may or may not be connected to each other depending on the setup.

    The motion you see in this video is 85% of what I'm trying to attain



    plus a few extra detailed movements to be added in afterwords.

    That's it, this is solid motion that follows a trail defined by the head, a very good start. The problem is, it deals with discrete spheres for some weird reason, instead of the much more desired single solid mesh.
     
    Last edited: Jun 10, 2023
  14. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    I'm guessing you would have a chain of Rigidbodies connected via some kind of appropriate joint.

    The bones would then simply follow the motion of the Rigidbody chain.

    It's basically the same process used for ragdoll physics.
     
  15. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,116
    I'm an old boy, I love the discrete spheres aesthetics for some reason. I actually really badly wanted to find a guy on Youtube who's been doing this approach and reached teh god mode of how good this looks (you actually can't tell the spheres apart, yet the animals he's making are all organic-looking), but I really can't remember who he was. If I find it in my history or he pops up again, I'll make sure to share. That S*** was brilliant. All I remember is that it was in Godot or sth like that. Oh I watch a ton of game-making videos, and have an archive of links for which I basically need a mini-Google to be able to search it.
     
  16. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,116
    And while we're at it, you can check out metaballs as a technique as well (or even ray marching). It would allow you to combine the two techniques (simulated chain-body + volumetric blending) to achieve a really interesting style of animation and rendering.