- Home /
The question is answered, right answer was accepted
[Unity v5.4] Aligned positioning not working when using Animations
Hi! I have a problem concerning Mecanim. I'm making a 2D game (probably not relevant to the problem) and within this game the user moves a GameObject aligned to a Grid. Everything works perfect, but it looks a bit stiff, because I position the GameObject by setting the desired location via (example code)
transform.position += new Vector2(1, 0); // move right
transform.position -= new Vector2(1, 0); // move left
transform.position += new Vector2(0,1); // move up
// and so on
So I decided to make some animations (for moving to the left, right, up and down), that change the position of the GameObject (with Apply Root Motion, Root Motion Curves, Booleans and corresponding transitions) - again, works great in theory.
I wrote some code to handle the Animations that works as expected but: the GameObject isn't moved completely in the desired direction. For example:
Animation MoveLeft:
Keyframe 1 positions: X=0 and Y=0
Keyframe 2 positions: X=-1 and Y=0
The GameObject moves in the desired direction but only 0.9 units, sometimes less.
I can't get it fixed but I guess, the animation isn't played until the end when Unity switches back to an "idle state", so that the GameObject doesn't reach the desired position (1 full unit). Or am I missing something? I'm nearly at the point to leave it that stiff because I can't get it to work as expected.
Every hint is more than welcome.
Best regards and thanks for reading.
Answer by guenni · Dec 19, 2016 at 08:42 PM
I fixed the problem by adjusting the distance (it doesn't "jump" 1 unit in the desired direction but something like 0.9 unit) and fixing the position with a script that's called at the end of the animation.
The problem is: I modify the position and the size (image a leaping frog viewed from above - I recreated the illusion of motion by changing the size halfway of the animation).
Follow this Question
Related Questions
Performance question - Animations in spritesheet vs animation in Mecanim? 0 Answers
Instant transition with Mecanim (Unity 2d) 4 Answers
2D-How to call a synced layer animation up in script (Animator) 1 Answer
Why is this C# script causing lag spikes between the game objects being cycled?, 1 Answer
sync color change of objects c# 1 Answer