- Home /
Moving BoxCollider2d with animation is not the right way?
Good morning, folks
I'm pretty new to Unity so certain patterns to make things are confusing me yet.
I'm creating a 2D game and in this case I want to create a simple "press" object that can press and damage player. You can watch video I've just uploaded here on Vimeo so you can fully understand my purpose.
So I've created some sprite (link here on Piskel, a animation (as you can see in video) and added a simple box collider that changes its offset during animation. As you can see in video, sprite and collider doesn't move properly synchronized and I think it's not a good idea to implements that object in that way, because collider should stay and it's the object transform that should move, but I can't figure why, so I'm asking to you which is in your opinion best workflow to implements this behaviuor.
Little corgi in video and me say "Thank you in advance"
Answer by RustyCrow · Jul 25, 2019 at 01:41 PM
Your video is set to private so can't watch it, BUT i think i get what you are trying to achieve.
So first i would recommend you check out the official tutorials for unity animations 2D so you understand the system better.
What you need to do is to create a Parent obj lets call it Thepress and then use this as your base game object.
Now you child(add to Thepress) the sprite(gameobject with sprite render), lastly you child a hitbox object(gameobject with 2d box collider).
From here you create a unity animation(i am assuming you know this) , add your sprite frames from your press animation and lastly you add your hitbox transform to the animation. What the transform will allow you to do is to animate the position of the hitbox, just sync it with your sprite frames and you should be done @Pilessio .
EDIT: @Pilessio After seeing your new public video i see the problem, The solution here would be to go to the curves tab in the animation window(lower left corner) and set all your Frams/keys to constant (Box select them all > right click > both tangent > constant). This will be more inline with with pixel frames, an instant shift per fram rather then the smooth motion.
Apologize for wasting your time, now I've just set video on Vimeo as public. Thank you for your answer
Np, i have updated my answer, check out the edit hopefully this will fix your issue.
Answer by I_Am_Err00r · Jul 25, 2019 at 01:39 PM
I tried watching the video, set to private, but I think I understand what you are experiencing; it sounds like you might even be using a 2D IK tool.
Unless you are moving the collider during the animation, what you are experiencing is the sprite is actually moving and the box collider and transform are staying in the same spot.
I work with an artist and I experience this all the time with them, but essentially it sounds like you are actually moving the sprite in it's local space when capturing keyframes.
The only things that should move in 2D animations are the IK points on the limbs and maybe bringing the top down for crouching (because crouching has a smaller collider) but other than that you want to move the sprite and capture the IK keyframes in the same position, even for things like jumping and sprinting (jumping and flying are the two my artist for some reason always sends with drafts having vertical local movement, and I have to remind them to stop doing that like literally every time).
If the animation keyframes show the sprite as changing local positions, it will capture that and make it appear like the box collider isn't following it, when it's the sprite not following the transform and moving around the local space.
Apologize for wasting your time, now I've just set video on Vimeo as public. Thank you for your answer
Your answer
Follow this Question
Related Questions
A problem regarding to player animation with checkpoint in 2D platformer 0 Answers
Jump animation will not play 1 Answer
2D animation diferent right and left 0 Answers
Animation Problem 1 Answer
Player Gets Stuck On TilemapCollider2D 0 Answers