Search Unity

How to change player's sprite and animation on collision?

Discussion in 'Getting Started' started by edmund20101, Dec 30, 2020.

  1. edmund20101

    edmund20101

    Joined:
    Oct 24, 2020
    Posts:
    7
    Hi guys, i am developing a 2d platformer android game on unity and i wan to change my player's sprite to auto equip the mask when the player collides to it, do you guys know how to do it?
    E.g. the player's sprite will show the mask equipped when it collides with the game object (mask)
     
  2. DimitriX89

    DimitriX89

    Joined:
    Jun 3, 2015
    Posts:
    551
    Making a pickup will need several steps, it isnt "out of the box" functionality. Detect a collision between the player and the mask, delete the mask prop from the scene, write in some "character status" script that you've picked up the mask, and finally update the character graphics accordingly.
    Detection is the easier part; you need a trigger collider on either player or pickup object, and then you run your detection related code from OnTriggerEnter method:
    https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter2D.html
    Displaying the equipped object on 2d character can be either easy or insanely hard depending on how this character is made. If it is a flat polygonal puppet with a skeleton, you can have the mask sprite be parented to the head bone, keep its GameObject disabled by default, and then enable it when it needs to be visible. If your character is drawn frame by frame, tough luck; you'll have to redraw the masked version of all the character animations.
     
  3. edmund20101

    edmund20101

    Joined:
    Oct 24, 2020
    Posts:
    7
    I have already drawn the masked version of character animations, the problem is that idk how to implement it into my character when it collides with the mask. I have searched youtube for tutorial but all i can find is changing the character sprite on collision which is not exactly what i want
     
  4. edmund20101

    edmund20101

    Joined:
    Oct 24, 2020
    Posts:
    7
    For the masked version of the character animations, do i need to connect them onto the animator controller as well?
     
  5. DimitriX89

    DimitriX89

    Joined:
    Jun 3, 2015
    Posts:
    551
    I havent worked with 2d animations to be honest. If controller behaves in any way similar to 3d, you can either create new animation tree on another layer and enable/disable it from script, or create second tree on the same layer and switch to its initial state using Animator.CrossFade.
    https://docs.unity3d.com/ScriptReference/Animator.CrossFade.html
    Note that 2d animations cannot be mixed together, so I am not sure how exactly layer weights or CrossFade transition duration will behave. In theory, toggling weights between 0.0 to 1.0 or setting translation duration to zero should give the desired effect.
     
  6. DimitriX89

    DimitriX89

    Joined:
    Jun 3, 2015
    Posts:
    551
    Does your character even use Animator component? I automatically assumed it does, could be wrong
     
  7. edmund20101

    edmund20101

    Joined:
    Oct 24, 2020
    Posts:
    7
    Yes, thx for the tip!
     
  8. edmund20101

    edmund20101

    Joined:
    Oct 24, 2020
    Posts:
    7
    sorry for the late reply,
    FYI, i already solved this by following this tutorial, thx for the help!