- Home /
Rigidbody 2D has velocity but isn't moving after Unity 5 upgrade
I want the player to have a speed equal to that of the screen scroll rate. I have a rigidbody2d whose velocity parameter I set directly every Update so the player moves along with the screen. In 4.6 this was working no problem but after upgrading to 5 for some reason the player stays still even though according to the log it still has velocity.
If I use AddForce in a FixedUpdate the player moves but the rigidbody2d.velocity parameter still says (0,0) so I can't compare it to the max velocity. Also, the whole game was coded so far just directly changing velocity so I'd like to keep that if possible.
Any ideas on what could be the problem? Or if this can't be fixed is there an easy way to use AddForce to result in a constant velocity?
void Update(){
//set the base player speed to the screen scroll rate
float xSpeed = scrollRate;
Vector2 newSpeed = GetComponent<Rigidbody2D>().velocity;
newSpeed.x = xSpeed;
//set the player's speed
//GetComponent<Rigidbody2D>().AddForce(newSpeed, ForceMode2D.Force);
GetComponent<Rigidbody2D> ().velocity = newSpeed;
print ("speed set:" + newSpeed);
print ("speed act:" + GetComponent<Rigidbody2D>().velocity);
}
EDIT:
Here's the code in Fixed Update:
void FixedUpdate() {
//set the base player speed to the screen scroll rate
float xSpeed = sm.realScrollRate;
Vector2 currentSpeed = GetComponent<Rigidbody2D>().velocity;
//set the player's speed
if(currentSpeed.x < xSpeed)
GetComponent<Rigidbody2D>().AddForce(Vector2.right * 250f, ForceMode2D.Force);
CONFIRMED ISSUE IS WITH UNITY 5
I have Unity 4.6 still on my PC and loaded a backup of my project and the player runs just fine when directly setting the velocity in Update().
Does anyone know what was changed in Unity 5 that would make this no longer function?
im having similar issues. im using the GetComponent().AddForce on click (or tap on mobile) when i click, ins$$anonymous$$d of adding force. the player instantly jumps to a new position. like transform.Translate would. also gravityScale on the rigidbody is pulling up ins$$anonymous$$d of down. im thinking of sticking with 4.6 for a while, too bad. i was looking forward to all the cool new features.
Does your player have an Animator on it? I think through a lot of trial and error I figured out what the problem was for me at least. If you disable "Apply Root $$anonymous$$otion" in the Animator component then the player should animate normally.
I'm guessing that the "Root $$anonymous$$otion" is trying to force the player to translate according to the animation but to be honest I haven't read the documentation on it as thoroughly as I should. If anyone is able to explain the Root $$anonymous$$otion concept better than the docs I would appreciate it.
I literally just figured this out myself and came back to tell you.
Answer by gagesaber · Mar 15, 2015 at 11:25 PM
Looks like the problem is that with the upgrade to Unity 5 all Animator components had the "Apply Root Motion" option enabled which I believe makes it so that the object moves according to it's animation rather than according to physics. Disabling this option allows the object to be manipulated by physics properly.
If I am misunderstanding the Root Motion concept I would appreciate any further explanation.
It helped me too! I didn't find any explanations about Root $$anonymous$$otion on Unity 5...
Just giving my own thanks; I can deal with my own errors, but figuring out these kinds of problems make me sweat profusely.
Answer by hoarfrost31 · Oct 19, 2017 at 10:02 AM
I had the same problem but also wanted to keep root motion on and turned out that problem was with Update mode and not root motion. Set Update mode to normal and you will be fine. If can't do that then you will need to set root motion on and off dynamically.
Cheers!
Your answer
Follow this Question
Related Questions
move 2d character affected by physics with velocity and/or add force 2 Answers
,Rotate a gameobject around another while being attracted by its gravity 1 Answer
Jumping with Rigidbody.AddForce not working? 1 Answer
I need Help with AdForce pls :( 1 Answer
Adding force to rigidbody2d to slide 1 Answer