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. Dismiss Notice

Making grass move when the character walks over it.

Discussion in '2D' started by richardthere, Jun 29, 2016.

  1. richardthere

    richardthere

    Joined:
    Mar 13, 2015
    Posts:
    19
    Hi,
    I am working on a 2d Platformer where everything is hand-drawn. I am an artist and this is my first game, so I am not that good on programming but until now it's working very fine and I am very happy learning C#.
    A big part of the game happens on a grass terrain and I would love if the grass would move a bit when the character walks over it. I made a screenshot and marked the grass so you can see what I mean. (Please dont consider the art now, it is just dummies for the game prototype)
    I was wondering if it would be possible to create a shader which affects the y point of the grass and make it shake when the character walks over. But as I said I am not good on programming and I never made a shader. The other idea that I had was to create an animation of the grass and play it just when the character walks over. But I believe this could be very expensive because there gonna be a lot of grass here and there.
    Do you have any ideas how could I approach this problem?
    Thanks in advance.



    Screenshot.jpg
     
    Last edited: Jun 29, 2016
  2. capnjake

    capnjake

    Joined:
    Nov 12, 2013
    Posts:
    53
    So there's a couple ways to do this. Firstly, are your images just single pieces (i.e. is one part able to move separately from another)? The grass bits are where this concern comes from because the horizontally drawn lines would rotate to if using the following method.

    Essentially, you'll want to know when the player comes in contact with another object that should be rotated. You can do this in various ways with the most simple being a BoxCollider2D or CircleColider2D marked as a trigger. For this to work, at least one object in the collision must have a Rigidbody2D component (this can be marked isKinematic if it is not what's powering the object).

    Create a script called RotateOnTouch (or something along those lines).

    In that script, which should be attached to an object you want to rotate, write the following code:

    Code (csharp):
    1.  
    2. public float rotateAmount = 20f; // Amount to be rotated
    3.  
    4. // This will handle when a player enters the collider on the plant or grass object
    5. void OnTriggerEnter2D(Collider2D other)
    6. {
    7.     // Check that the colliding object is the player object
    8.     if (other.gameObject.tag == "Player")
    9.      
    10.     StartCoroutine(RotateMe(Vector3.right * rotateAmount, 5));
    11.     // Call the rotation
    12. }
    13.  
    14. // This will handle when the colliding object leaves the plant or grass bit
    15. void OnTriggerExit2D(Collider2D other)
    16. {
    17.     // Check that the colliding object is the player object
    18.     if (other.gameObject.tag == "Player")
    19.      
    20.     StartCoroutine(RotateMe(-Vector3.right * rotateAmount, 5));
    21.     // Call the rotation
    22. }
    23.  
    24. IEnumeratorRotateMe(Vector3 byAngles, float inTime)
    25. {
    26.     Quaternion fromAngle = transform.rotation;
    27.     Quaternion toAngle = Quaternion.Euler(transform.eulerAngles + byAngles);
    28.    
    29.     for (int t = 0f; t < 1; t += Time.deltaTime / inTime)
    30.     {
    31.         transform.rotation = Quaternion.Lerp(fromAngle, toAngle, t);
    32.         yield return null;
    33.     }
    34. }
    35.  
    36.  
     
  3. richardthere

    richardthere

    Joined:
    Mar 13, 2015
    Posts:
    19
    Thank's a lot. I understand your script and I put it on a flower and it works. But now the flower is rotating on the X axis and I would like to rotate it on the Z axis. I tried to change the script but I couldn't find a way to make it work.
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    Try changing Vector3.right to Vector3.forward in both spots.
     
  5. richardthere

    richardthere

    Joined:
    Mar 13, 2015
    Posts:
    19
    Thank's I actually created a new Vector3(0,0,1) and it worked. But I don't like the effect very much. It's very artificial.
    Now I am doing the animation frame by frame and playing the animation when the character walks over the grass. And it looks fine!
     
    capnjake and LiterallyJeff like this.
  6. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    Glad to hear you got it working. Just FYI, Vector3.forward is shorthand for "new Vector3(0,0,1)" but doesn't create a new vector3 struct.
     
    Last edited: May 24, 2019
    theANMATOR2b and richardthere like this.
  7. capnjake

    capnjake

    Joined:
    Nov 12, 2013
    Posts:
    53
    That's kinda what I thought. The drawing out of the animation should look nice!