- Home /
Rigidbody2D.velocity not moving the object
I have this gameObject
that was moving left and right with yield
. It was working perfectly fine. But now even it gets the value, it doesn't move.
So before it was like this I added some children to it and made some animations to these children with this parent gameObject
's animator and script. I don't know if that is the reason to it, but now it is not moving with this function. So some people say you should use AddForce instead of direct velocity, But it also didn't work.
What should I do?
P.S: I already had some animations on this parent gameObject
before.
private void Walk()
{
if (readyToWalk && health > 0)
{
animator.SetBool("Walking", true);
if (health >= 40)
{
moveSpeed = 7;
//yield return new WaitForSeconds(0f);
}
if (health < 40 && health > 0)
{
moveSpeed = 10;
}
if (IsFacingRight())
{
myRigidBody2D.velocity = new Vector2(moveSpeed, 0);
}
else
{
myRigidBody2D.velocity = new Vector2(-moveSpeed, 0);
}
}
}
Answer by fuadshahmuradov · May 30, 2020 at 09:36 AM
I just realized as I have added a new Animation to my GameObject (which broke my movement), I have set a position for the GameObject inside Animation. That's why it was stuck in a position. I removed it from the Animation, and it moves now. Nothing wrong with velocity or Rigidbody2D.
This should mean that your animation is controlling the movement ins$$anonymous$$d of your rigid body physics. Which can only happen if you have marked APPLY ROOT $$anonymous$$OTION checked. In our suggestions we said you to try unchecking APPLY ROOT $$anonymous$$OTION to see if object still moves but you reported it did not move after unchecking also. I did not understand that. If the problem was with the object position through animation, then unchecking APPLY ROOT $$anonymous$$OTION should ignore the movement through animation and should give controll of movement to rigid body physics code.
@GameHourStudio , I don’t have Apply Root $$anonymous$$otion checked.
Answer by SteenPetersen · May 30, 2020 at 07:25 AM
You should only be using WaitForSeconds inside a coroutine.
Try to debug.log whether your readyToWalk boolean is actually true, like so:
private void Walk()
{
Debug.Log(readyToWalk);
if (readyToWalk && health > 0)
{
animator.SetBool("Walking", true);
if (health >= 40)
{
moveSpeed = 7;
//yield return new WaitForSeconds(0f);
}
if (health < 40 && health > 0)
{
moveSpeed = 10;
}
if (IsFacingRight())
{
myRigidBody2D.velocity = new Vector2(moveSpeed, 0);
}
else
{
myRigidBody2D.velocity = new Vector2(-moveSpeed, 0);
}
}
}
It was a coroutine before. I saw it is not working and I am not actually using yield, I removed it and changed it to function.
About debugging, I tried and saw it goes till else
part, and even changes velocity, but object doesn't move.
Check your rigidbody for the following:
$$anonymous$$ake sure it is not set to IsKinematic
$$anonymous$$ake sure it does not have a large $$anonymous$$ass
$$anonymous$$ake sure it does not have large Drag
$$anonymous$$ake sure it has no constraints on it.
I checked. I have none of those problems :(
Answer by KoenigX3 · May 30, 2020 at 08:17 AM
Do you have an Animator on the GameObject? Have you tried unchecking Apply Root Motion and checking Animate Physics?
I have an Animator on the parent GameObject, in which I have all the animations. Apply Root was unchecked already. And I tried with Animate Physics, still doesn't work..
Answer by WaqasHaiderDev · May 30, 2020 at 08:27 AM
Hi, first of all I would like to know why are you using Rigid Body? If you really need rigid body for collision detection, then my next question is why are you trying to move the object using velocity or add force? Why don't you move your object using
transform.Translate(Vector3.forward * speed * time.deltaTime)
or for 2D game, use 2D vectors instead of 3d vector. Also don't forget to set velocity in update function if you want object to keep moving because
Rigidbody.velocity = new Vector3(0, 0, velocity);
sets velocity only once. But still your object should move atleast once. (try setting very high velocity to verify if its mass inertia which is notletting object move, if you have not tested it already). At end, I have just tested and found that
Rigidbody.velocity = new Vector3(0, 0, velocity);
does not work if the animator has been marked as APPLY ROOT MOTION because in that case motion will be updated on the basis of root bone movement from the animation clip and not by the unity physics. Turn it off and tell if it still does not move. And I would also suggest use addforce instead of velocity setting approach because add force will take into account velocity variations on collisions making them realistic but velocity setting will always set the same velocity value no doubt what type of collision happens.
I tried almost all of these. And when I noticed 1 thing in 2 conditions.
When I raised the velocity very high
When I called
Walk()
function insideUpdate()
Its body moved but the sprite was in the same position. I am not sure where or how fast did body (I think collider) moved because I couldn't see it. But I know it did because it hit and pushed the object in front of it.
Edit: I tried to debugging the name of the object that is colliding and pushing. And it was GameObject which is the parent. But I see none of its components move. But it somehow collides with other object. I don't understand
Whenever call something in update function, multiply it with time.deltaTime because otherwise the movement speed will be very high and also it will make movement frame rate independant and object will move same distance in same time no matter how much FPS is. Co$$anonymous$$g back to your question, I will suggest try to experiment by moving your parent object with
transform.position += new Vector2(movespeed *time.deltaTime, 0)
and check that if transform and all the components along with childrens move or not. So that you may get clear if it is problem related to your code or scene objects. Once you get your problem debug, then you can later move with velocity also.
I found the answer. Thanks for your help though!