- Home /
Problem with character movement 2d when attaching animator
I made a character using spritesheet animation concept, than decision was made to switch to animating character using separate body parts like in this tutorial: Bone-Based Unity 2D Animation: Introduction
The character made using spritesheet animation is working perfectly fine. But with the new character i am encountering strange problems. I am trying to move both characters with the same script and attaching the same components to them:
Circular colliders at feet, some other collider on top.
Both have Rigidbody2d component attached to them. Fixed Angle is checked, Is Kinematic is unchecked.
The same script to control movement is attached to both characters.
Two different animator components because of different nature of the animation. In the first case animation was made manually in photoshop as a spritesheet. In second case, there was a file with body parts, and annimation was made in unity using mecanim.
The initial conditions are: the character is placed a little above the platform, so when the game starts, it should first fall and touch the ground, then walk on the platform.
I encounter following problems with character made with body parts:
When Animator component is not attached or disabled, character is moving correctly, falling to the ground correctly, as determined by Rigidbody2d component. But of course no animation is happening.
When I enable Animator component with the controller and all the animations i made using mecanim, character stays in mid air, and does not move anywhere, does not respond to controls, but flipping the character works, and proper animations show when trying to walk to one direction or another, but there is no actual movement. Update mode on the animator is set to Normal, Apply Root motion is unchecked, but checking it does not make any difference. Culling mode is Always Animate. The same settings work with the first character.
When Setting update mode to Animate Physics, the Character start to jiggle heavily. It is also now trying to fall to the ground, but after lowering a little bit, returns to its initial position above the platform.
I tried using following code to move the character (both variations work on the first character (one that uses spritesheets)):
rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
Or
transform.position += transform.right * move * maxSpeed * Time.deltaTime;
I feel like I am missing something out, or did something wrong with animations on the second character. Any ideas?
I have found a similar question here: http://answers.unity3d.com/questions/648238/2d-animator-component-makes-my-player-stuck-in-spa.html
It says checking or unchecking Apply Root motion solves the problem, but this does not work in my case.
I have also tried this: disable animator component, so that i can walk, move character on the platform, than in the middle of the game enable Animator. If I do so, the character teleports to the point where it was initially standing when I launched the game. The animation starts playing and showing properly if i press controls, but the player does not move.
Answer by vovo801 · Mar 18, 2015 at 08:09 PM
I have figured out the solution that worked for me.
First, I have created an empty game object and parented my game object to this newly created one.
Second, moved all the components from my first object to its parent, except for Animator component.
And it worked!!! Also had to rewrite my code a bit. When searching for Animator component had to change function GetComponent for GetComponentInChildren.
Hope this helps someone.
Your answer
Follow this Question
Related Questions
2D Animation does not start 1 Answer
Damn, HOW CAN I KNOW, IF MY 2d ANIMATION SPRITE IS FINISHED??!! 0 Answers
Best way to do animate soldier? 1 Answer
Animation transform lock 0 Answers
Not Run while Aiming; Slope Terrain Making Object Faster 0 Answers