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 tedbox39 · Nov 17, 2015 at 02:29 PM · rigidbody2dvelocityphysics2d

Rigidbody2D returning zero values

I am trying to access the velocity value of a Rigidbody2D component and it is always returning zero values (0, 0). I have tried this in the FixedUpdate, Update and LateUpdate methods and all give the same result.

The Rigidbody2D has the following values:

  • Mass = 1;

  • Linear Drag = 0;

  • Angular Drag = 0;

  • Gravity Scale = 0;

  • Is kinematic = false;

  • Interpolate = none;

  • Sleeping mode = never sleep;

  • Collision detection = Discrete;

  • Constraints freeze position; x=false, y=false;

  • Constraints freeze rotation; z=true;

These settings are so that the object drifts through space at a constant speed and isn't affected by gravity. The initial force to get it moving is applied using the AddForce() method.

I am accessing the velocity using the following code (I am writing an asteroids-style game and want to spawn smaller asteroids when larger ones are hit):

 foreach (GameObject ast in collided) {
     // Spawn new asteroids if appropriate, i.e. large makes 2 mediums, medium makes 2 smalls.
     if (ast.GetComponent<AsteroidControl>().Type != AsteroidData.Small.type) {
         Rigidbody2D rb2D = ast.GetComponent<Rigidbody2D>();
         if (ast.GetComponent<AsteroidControl>().Type == AsteroidData.Large.type) {
             this.generateRocks(AsteroidData.Medium.type, ast.transform.position, rb2D.velocity);
             this.generateRocks(AsteroidData.Medium.type, ast.transform.position, rb2D.velocity);
         } else {
             this.generateRocks(AsteroidData.Small.type, ast.transform.position, rb2D.velocity);
             this.generateRocks(AsteroidData.Small.type, ast.transform.position, rb2D.velocity);
         }
     }
 }

I have also tried setting 'simulated = true' (this was false when examining the object in debugger - not sure if that's relevant?), but still get zero values.

I am running Unity 5.2.2f1 Personal Edition.

I've looked at other questions with this problem, but can;t see an answer that would address the problem. Does anyone have any ideas how I can fix this?

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

1 Reply

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

Answer by MelvMay · Nov 17, 2015 at 07:04 PM

A Rigidbody2D defaults to being simulated. If it's off then it must've been turned-off. This indicates something is happening you've not accounted for perhaps.

The code above doesn't really help in understanding the problem. You talk about a single object but the code above is iterating some group of objects.

Have you tried making things a little simpler to debug. When you add a force, the velocity won't instantly change the velocity unless you add the force as an impulse. Also, if you want to set an initial speed, you are free to set the velocity directly.

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 tedbox39 · Nov 19, 2015 at 05:53 AM 0
Share

I am so glad I added the bit about 'simulated=false'. You have led me to the answer... I have a collision manager class that handles all collisions and in that I was perfor$$anonymous$$g 'gameObject.SetActive(false)' on each asteroid that had collided with something before adding it to the collided list in a different class (asteroid manager), which my code snippet above is taken from. Doing this obviously kills the physics simulation and zeros the velocity.

Apologies for the snippet - it was hard to know what to post since I have a lot of code; I just posted the bit where I was experiencing the problem. The loop iterates over a set of collided asteroids (all gameobjects with the same components including Rigidbody2D) and triggers the generation of new smaller ones in the game. I described what was happening in the singular just to make the language easier.

Anyway, thanks @$$anonymous$$elv$$anonymous$$ay! I thought I was stuck there for a bit. :)

ps I meant to reply to your comment, but this has been added as an answer for some reason. $$anonymous$$aybe because I am new to the site? Anyway, if you can think of a suitable answer to post I will accept it as the correct one.

avatar image MelvMay ♦♦ tedbox39 · Nov 19, 2015 at 08:35 AM 0
Share

Glad it helped. Good luck with your project!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Android velocity slower than editor 0 Answers

How to get the speed of an object? 3 Answers

Scaling an object causes slow movement (Since Update 5.5.4) 1 Answer

How to move a rigidbody2D at a constant speed without AddForce? 1 Answer

After AddTorque, the resulting angularVelocity depends of the RigidBody's velocity? 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