- Home /
Character not moving on Unity 5
I've ported a 2d platformer project into Unity 5. After finally getting the project to compile, everything works, except the basic movement for my player, it doesn't move or jump, though the animations are correctly triggered.
Here is the code for moving and jumping:
public void Move(Direction direction)
{
if (CurrentDirection != direction)
{
ChangeDirection();
}
if (MoveAllowed(direction))
{
_rigidbody2D.velocity = new Vector2(Velocity * (int)direction, _rigidbody2D.velocity.y);
_isRunning = false;
}
}
public void Jump()
{
if (CanJump)
{
_rigidbody2D.AddForce(new Vector2(0f, JumpForce * _rigidbody2D.mass), ForceMode2D.Impulse);
}
else
{
OnCollidingOnCeiling(_entityTransform.renderer.bounds.max);
}
}
The game runs this code as usual, but it seems that changing the velocity or adding a force no longer works. Why is this happening? Has something drastically change in Unity 5 about 2D rigidbodies?
EDIT: A few things I forgot to mention:
The _rigidbody2D is referencing the parents rigidbody2D.
_rigidbody2D shows when the program gets to those lines, so it doesn't look like a problem of missing references.
I'm using UnityVS with Visual Studio Community Edition 2013, dunno if it matters.
You realize that if you append a script to an object that you don't need a _rigidbody2D reference, as you can directly call rigidbody2D (lowecase) to get in on the appended object? Same works if you want to reference the parents rigidbody2D with parent.rigidbody2D etc. This works for ALL items inherited from GameObjects.
Also, please put in the $$anonymous$$oveAllowed() and ChangeDirection() functions.
I know I dont' need it, I do it to avoid Unity constantly calling GetComponent() for that rigidbody2d.
Actually the _rigidbody2D variable is referencing the parent rigidbody2D.
Answer by ciwiaf · Mar 05, 2015 at 11:59 AM
this happened to my project too. Apparently, the "Apply Root Motion" in the Animation got enabled.. Try disabling that again.
Solved!!! Thank you very much, now it's moving even though there are still some weird things that should not be happening...
Answer by Goa · Mar 05, 2015 at 11:21 AM
You need to GetComponent in unity 5 Explained here: http://blogs.unity3d.com/2014/06/23/unity5-api-changes-automatic-script-updating/
As a rule of thumb I always get the components I need in Start(), and make them private variables, so I'm already doing it:
_rigidbody2D = GetComponentInParent<Rigidbody2D>();
The thing I forgot to mention is that the rigidbody2D is from the parent, but I don't think it matters anyway, the VS inspector shows the rigidbody of the parent at runtime.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
InvokeRepeating time doesn't sync within the same update function 2 Answers
Instantiated object not showing in scene or hierarchy 2 Answers
My balance in the game and my multiplier are not saving, i cant figure out why, any help? 0 Answers