Search Unity

2D platformer that changes collider

Discussion in '2D' started by DamKay, Oct 7, 2019.

  1. DamKay

    DamKay

    Joined:
    Aug 18, 2019
    Posts:
    10
    I haven't seen anyone talk about that, please help!
    In a 2d platformer when a player with long legs jump, the collider shrinks up.
    When he touches the ground in a shrink jumping state, he needs to stretch back down and so the player must be moved upwards.
    Whats the best practice to implement it? or really any practice?
     
  2. Ledesma099

    Ledesma099

    Joined:
    May 15, 2018
    Posts:
    26
    So, do you want to change the size of the collider when the player jumps?
    To do this, change the size of the collider through Animation in the frames you need. In Animation, you can add properties such as position, scale, rotation of your object or size and position of the collider, etc. And change your values through this
     
  3. DamKay

    DamKay

    Joined:
    Aug 18, 2019
    Posts:
    10
    Thanks for the reply but its not what i meant, i want to change the collider, one way is as you said- change with animation.
    The problem is when the player hits the ground the animation changes revert to the normal size and offset and then the player stuck in the ground - he needs to move up - how is that possible to know how much and move him via script and colliders info etc
     
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @DamKay

    You should post and image or images of your setup. Someone might be able to help, instead of trying to guess what you try to describe. To me it sounds like it is something that can be handled if properly done, but I have no idea how much rigging and character setup experience you have.
     
    vakabaka likes this.
  5. DamKay

    DamKay

    Joined:
    Aug 18, 2019
    Posts:
    10
    @eses Alright:
    My character is made up from one png file using 2D animations.
    33.PNG
    It has a 2D box collider (not optimal but doable for me) and a player script as well as input script.
    The flow control is that the update gets the input and fixed update uses the input to determine the movement needed in terms of Vector2 force, then it passes the force to a module i made that take checks if the force can be applied- no obstacle prevents it, and moves the player object according to that.
    What i meant by collider changes with animations is the example of the following fall animation:
    fall animation.gif
    The problem starts when the player hits the ground while in this animations,
    the script detects take the player is touching the ground and then the animator turns the animation to idle again but the player also needs to move up according to the shrink the fall animation made:
    fall to idle.gif
    I can make the animation call the player script and pass on its collider info if it helps.
    Hope its clearer now, i can answer any question as long as someone actually succeed to help
    And thank you for your reply!
     

    Attached Files:

  6. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    You can try to let the collider stay the same size and put the sprite renderer on the child object and move it on the y-axis depended on the animation and colliders size.
    Or, everything is relative, move the center (sprite pivot) at the foot-ground and change(or don't change) the size from the top. I mean, find the point which shouldn't move.
     
    Last edited: Oct 8, 2019
  7. DamKay

    DamKay

    Joined:
    Aug 18, 2019
    Posts:
    10
    Thanks for the reply and suggestion but i really want to learn how to work with changing collider for even more kinds of animations like slide (which will change the collider to almost 90 degrees)
     
  8. Ledesma099

    Ledesma099

    Joined:
    May 15, 2018
    Posts:
    26
    It's hard to change the collider through script. Maybe there is a better way to do it.
    But I give this idea just for the problem of the animation.
    It's theory. But when the player jumps, save in a variable jump = true. Then get the value of the jump animation parameter in the Update method. And if this value is equal to false and the jump variable is equal to true. Then put the animation with the effect you want to achieve or extend the jump animation to achieve your goal (The latter I see complicated, because it would be more imprecise).
    I hope it is understood.