Trouble with Key Frame Animations

Background: Instead of the glitch garden assets, me and a friend are creating Food vs. Fans, where fans are the attackers. I am making a stand up fan jump into the air and rotate 90 degrees to act as if it is smashing the defender. Stopping works while attacking.

Issue: during the in-game time, the attacker will slowly move a tiny bit up and a tiny bit to the left. I checked that the speed of the attackers remains 0. I checked that the first and last frame of the attack animation is the same (0,0) so there is no movement. I double-checked the transition of animation and that the animation wasn’t playing a different animation at the end of each loop.

Some more observations: If I set the x and y in the final frame to be 1 unit in the opposite directions (down and to the right) it will move down and to the right. A temporary solution would be to find a balance to counteract the movement, but I would like to figure out the core issue to improve my understanding and know that this issue will not resurface later on.

Lastly, the movement to the left and upward changes, it’s not always constant. For example, the movement is sometimes shifting to the left by .01, sometimes it’s by .1 so my suggested solution above wouldn’t even completely fix it.

Thanks for your time!

Hi Joshua,

Welcome to our community! :slight_smile:

Could it be that your enemy is colliding with a defender? I am referring to an actual “physical” collision between two non-trigger colliders. That could explain why the enemy automatically moves although you do not give it any velocity via code and although the animation does not move it. If there is a “physical” collision, the physics simulation might be causing the problem.

I currently am not placing any defenders on the board, but the same behavior is being observed. I do have a collider on the field to detect clicks on the field, so I may look into that. I will play around and see what I can do, but assuming this is the problem, what would be a solution?

Update: Just tried testing to make sure the attacker was outside the defender spawner area collider and the same behavior was observed. Is there a way to check if the physics engine is the cause of this via code or some tool inside Unity?

The ground has got a collider, too, doesn’t it? Maybe the attacker is colliding with the ground. Use the OnTriggerStay2D method and log the name of the game object which collides with your enemy into the console. That would be the fastest way to figure out if the physics simulation is causing the problem.

Make sure the background collider is a trigger collider, not a “solid” collider.

Great idea! I looked into this quite a bit and realized I do not have any rigid bodies on any of my attackers. This was intentional as kinematic rigid bodies messed up the animation so they were going backwards or just falling off screen. Taking off rigid bodies was the easy solution.

Since my attackers do not have any rigid bodies, the ontriggerstay and oncolliderstay methods and their counterparts all do not work- at least in the capacity I have been using them. I tried changing the trigger option on all my characters and any combination and I still observe this behavior.

I will add that my attackers move straight in their respective lanes until I check “true” for my “Attacking” bool within the animator. This is when the strange behavior begins.

Thank you for your help and suggestions! These ideas are good to go through and will help me for future bug fixing!

UPDATE:
I continued forward with the course rigging together a temporary solution that has my attacker moving towards the point where it stopped. The animation looks bad now, but he does not move. With my second attacker, I am not facing the same things as with my first attacker. I have added the same components and they have the exact same scripts (for now) yet this second attacker does not move forward. I tried making it so the first attacker with the weird behavior teleports to its original location at the end of the animation each time, but it does not seem to work (for the five minutes I attempted it) and just went with my current solution so I could move on.

For reasons of performance, add Rigidbody2D components to all moving game objects with Collider2D components attached.

If you animate a sprite, move the visual parts to a child game object and animate the child. The parent must not be animated. You may move the parent via code but not the animated child. If you try to animate and move the same game object, you might end up with undesired side effects.

I’d suggest to fix this first before proceeding with your project to avoid any future problems in this respect.

1 Like

Rigid bodies attached and I followed your advice- it works! Sometimes, working ahead, I see those steps and think I can work around them, but if I have learned anything- listen to instructions! This has taught me best practice and shown me first-hand how those small things can mess up everything.

Thank you so much for your time! I really appreciate your help! It is so good being a part of this community :slight_smile:

In my personal opinion, the best way to learn something is to explore things yourself and to listen to instructions. The instructions are limited to the experience of another person but to become good at something, you will also have to gain your own experience in a field. Since nobody knows everything, there is always a chance that you will come up with an alternative or even better solution. Instructions are great if you do not find a solution yourself or if you want to compare your approach to another one.

Keep up the good work! :slight_smile:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms