Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by fuadshahmuradov · May 30, 2020 at 07:19 AM · 2drigidbody2drigidbody.velocity

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);
             }
         }
     }




Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image WaqasHaiderDev · May 30, 2020 at 09:59 AM 0
Share

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.

avatar image fuadshahmuradov · May 30, 2020 at 11:59 AM 0
Share

@GameHourStudio , I don’t have Apply Root $$anonymous$$otion checked.

avatar image
0

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);
              }
          }
      }


Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fuadshahmuradov · May 30, 2020 at 07:30 AM 0
Share

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.

avatar image SteenPetersen fuadshahmuradov · May 30, 2020 at 07:34 AM 0
Share

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.

avatar image fuadshahmuradov SteenPetersen · May 30, 2020 at 07:43 AM 0
Share

I checked. I have none of those problems :(

avatar image
0

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?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fuadshahmuradov · May 30, 2020 at 08:31 AM 0
Share

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..

avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fuadshahmuradov · May 30, 2020 at 08:45 AM 0
Share

I tried almost all of these. And when I noticed 1 thing in 2 conditions.

  1. When I raised the velocity very high

  2. When I called Walk() function inside Update()

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

avatar image WaqasHaiderDev fuadshahmuradov · May 30, 2020 at 09:25 AM 0
Share

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.

avatar image fuadshahmuradov WaqasHaiderDev · May 30, 2020 at 09:37 AM 0
Share

I found the answer. Thanks for your help though!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

272 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rigidbody2D.velocity is not working. 0 Answers

2D objects gets stuck on the ground/wall? 12 Answers

2d physics for cars. The game from the top down 1 Answer

Rigidbody2d addforce isn't moving character 1 Answer

Collision of child is detected as collision of parent with Rigidbody2D 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges