Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Vert Animation

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

  1. tobiasparrow

    tobiasparrow

    Joined:
    Jul 19, 2012
    Posts:
    38
    Hi!

    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>();
    2.  
    3.         var verts = meshFilter.mesh.vertices;
    4.  
    5.         verts[3].x = 0.5f + offset * BEND_FACTOR / transform.localScale.x;
    6.         verts[2].x = -0.5f + offset * BEND_FACTOR / transform.localScale.x;
    7.  
    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. https://docs.unity3d.com/ScriptReference/Sprite-vertices.html

    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.

    Thanks!
     
  2. zioth

    zioth

    Joined:
    Jan 9, 2019
    Posts:
    87
    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

    tobiasparrow

    Joined:
    Jul 19, 2012
    Posts:
    38
    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

    GameDevCouple_I

    Joined:
    Oct 5, 2013
    Posts:
    1,528
    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!

    https://twitter.com/devolverdigital/status/594299704797597696

    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

    jeffreyschoch

    Joined:
    Jan 21, 2015
    Posts:
    2,461
    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:
    https://twitter.com/minionsart/status/1014226452378775557

    direct to the video:
    https://video.twimg.com/tweet_video/DhM_hDNWAAEXrYX.mp4

    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):
    https://bitinn.github.io/ScriptableRenderPipeline/ShaderGraph/Rotate-About-Axis-Node/

    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

    tobiasparrow

    Joined:
    Jul 19, 2012
    Posts:
    38
    @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.