Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Vert Animation

Discussion in '2D' started by tobiasparrow, May 23, 2019.

  1. tobiasparrow


    Jul 19, 2012

    I just want to know if this is possible. I have interactive grass that moves when the player passes over it. I am using a quad, but I really want to use a sprite renderer. I have this code to modify the quad.

    Code (CSharp):
    1.         meshFilter = this.GetComponent<MeshFilter>();
    3.         var verts = meshFilter.mesh.vertices;
    5.         verts[3].x = 0.5f + offset * BEND_FACTOR / transform.localScale.x;
    6.         verts[2].x = -0.5f + offset * BEND_FACTOR / transform.localScale.x;
    8.         meshFilter.mesh.vertices = verts;
    I tried using a sprite renderer, but I don't think I understand exactly what I am doing. I tried using this link.

    Is there any advice or the right direction someone can point me in or if it is even possible. I don't want to use bones to animate it.

  2. zioth


    Jan 9, 2019
    Does the grass move in the exact same way every time the player moves over it? Or are there maybe 2-3 ways it can move? If so, you can use a sprite animation, triggered on collisionenter. If it's more complicated than that (responding to player speed, behaving differently if the player stands on the grass for a while, etc), then it will be harder to do with sprites.

    I've never done anything like this myself, so I can't guarantee I'm giving good advice. :)
  3. tobiasparrow


    Jul 19, 2012
    Yeah I was looking into bone animation. Thanks for the information though! :) I just was wondering if I was doing something wrong with the sprite verts or it wasn’t possible.
  4. GameDevCouple_I


    Oct 5, 2013
    Is there a reason it needs to be a sprite rather than a quad? I only ask in case you werent aware that Mixing 2D and 3D is absolutely fine, a lot of games do this secretly.

    Enter the gungeon looks like old school 2d pixel art but is actually 3d for example!

    In fact you can actually pull of some effects only possible doing it this way, we ourselves are making a game that mixes 2D and 3D in a way similar to "the last night" and "octopath traveller", and we have a bunch of sprites mixed with quads all over the place, the performance is good and so far no problems :)
  5. jeffreyschoch


    Jan 21, 2015
    Frequently I've seen this effect done with a vertex shader. You can pass the player's current position into the shader, and it will offset the sprite verts based on the distance to that position.

    Here's an example of that done in 3D, doing it in 2D would be very similar:

    direct to the video:

    For 2D, i'd recommend perhaps rotating all the vertices around the pivot point based on the pivot's distance from the player, rather than pushing per-vertex to avoid strange warping.

    I frequently take snippets from the new ShaderGraph nodes (you may even be able to write the whole shader in shadergraph):

    I realize shader programming is not easily accessible or familiar to most, but I figured I'd mention the technique.
    Last edited: May 25, 2019
  6. tobiasparrow


    Jul 19, 2012
    @GameDevCouple_I Thanks for mentioning that! I definitely feel better about using them in my game. I guess I am sort of a purist but I will just stick with what I have (or try shader graph).

    @jeffreyschoch Thanks for that post! I will try and see if I can make something in shader graph.