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

How can I make the platform shake and change its sprite before it falls?

Discussion in '2D' started by Dumitru, Nov 18, 2015.

  1. Dumitru

    Dumitru

    Joined:
    May 7, 2015
    Posts:
    80
    Hello, so I am making a 2D platformer and I was wondering how I could make the platform shake ( and the character that is on it shake along with it ) before it falls?

    The platform has a timer of 5 seconds, what I am trying to achieve is this

    When the player jumps on the platform, the platform starts to shake and changes its sprite from platform sprite to decayed platform sprite before it falls.

    Bonus points for those who help me / gives an idea on how to add the small particle to the decayed platform that falls from it before the platform falls itself.


    Here's the script that I have wrote to make the platform fall, it has a timer.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FallingPlatform : MonoBehaviour
    5. {
    6.     private Rigidbody2D rb2d;
    7.  
    8.     public float fallDelay;
    9.    
    10.     void Start()
    11.     {
    12.         rb2d = GetComponent<Rigidbody2D>();
    13.     }
    14.  
    15.     void CollisionEnter2D(Collider2D col)
    16.     {
    17.      if (col.GetComponent<Collider>().CompareTag("Player"))
    18.      {
    19.          StartCoroutine(Fall());
    20.      }
    21.     }
    22.  
    23.     IEnumerator Fall()
    24.     {
    25.         yield return new WaitForSeconds(fallDelay);
    26.         rb2d.isKinematic = false;
    27.         GetComponent<Collider2D>().isTrigger = true;
    28.         yield return 0;
    29.     }
    30. }
     
  2. LiberLogic969

    LiberLogic969

    Joined:
    Jun 29, 2014
    Posts:
    138
    First off, I believe void CollisionEnter2D(Collider2D col) should be void OnCollisionEnter2D(Collision2D col).

    When it comes to shaking the platform, I would just make an animation. Add the different platform sprites as children of the platform (as in, not attached to the GameObject that contains the collider2D). Only animate those objects so you avoid having weird physics reactions to anything standing on top of the platform. You can "change" the sprite by simply enabling the GameObject with the proper sprite in each animation (and disabling the other sprites). With this setup you could pass your timer value into your animator and use that value to determine which animation should be playing. You could also child a ParticleSystem to your platform and enable it within the last "decaying" animation.

    Maybe your script would look something like this :

    Code (CSharp):
    1.     using UnityEngine;
    2.     using System.Collections;
    3.  
    4.     public class FallingPlatform : MonoBehaviour
    5.     {
    6.         private Rigidbody2D rb2d;
    7.         private Animator animator;
    8.         private bool collidingWithPlayer = false;
    9.  
    10.         private float fallTimer;
    11.         private float refreshTimer;
    12.  
    13.         public float fallDelay;
    14.  
    15.         // how long before the platform can reset its decayed state?
    16.         public float refreshDelay;
    17.  
    18.         void Start()
    19.         {
    20.             rb2d = GetComponent<Rigidbody2D>();
    21.             animator = GetComponent<Animator>();
    22.  
    23.             fallTimer = 0;
    24.         }
    25.  
    26.         void OnCollisionEnter2D(Collision2D col)
    27.         {
    28.                 if (col.gameObject.tag == "Player")
    29.                 {
    30.                       collidingWithPlayer = true;
    31.                 }
    32.         }
    33.         void OnCollisionExit2D(Collision2D col)
    34.         {
    35.                 if (col.gameObject.tag == "Player")
    36.                 {
    37.                       collidingWithPlayer = false;
    38.                       refreshTimer = 0;
    39.                 }
    40.         }
    41.  
    42.         void Update()
    43.         {
    44.                if (collidingWithPlayer)
    45.                {
    46.                      // start adding up how long we have been on the platform
    47.                      fallTimer += Time.elapsedTime;
    48.                    
    49.                      if (fallTimer > fallDelay) // if its been enough time....
    50.                      {
    51.                            // make the player fall
    52.                            rb2d.isKinematic = false;
    53.                            GetComponent<Collider2D>().isTrigger = true;
    54.                      }
    55.                }
    56.                else
    57.                {
    58.                    // otherwise we wait for a little bit before regenerating the platform...
    59.                     refreshTimer += Time.elapsedTime;
    60.                     if (refreshTimer > refreshDelay)
    61.                     {
    62.                          fallTimer -= Time.elapsedTime;
    63.                          if (fallTimer < 0)
    64.                              fallTimer = 0;
    65.                      }
    66.                }
    67.               // give the animator the fallTimer value so it can transition between the proper animation states...
    68.               animator.SetFloat("FallTimer", fallTimer);
    69.         }
    70.     }
    71.  
    This is obviously just an example, and you will have to make the animations and set up the transitions between states based on the timer parameter. Hope it gives you some ideas :)
     
    Last edited: Nov 18, 2015
  3. Dumitru

    Dumitru

    Joined:
    May 7, 2015
    Posts:
    80


    I did use OnCollisionEnter2D(Collision2D col) in the version of Unity 5.2.2f1 (64-bit) and it gave me an error :S

    Also, if possible, can you explain in more details on how to make the platform change its sprite as soon as the character touches it? I'm still a newbie on Unity so any help will be greatly appreciated, thank you for your time!
     
  4. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    832