- Home /
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?
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.
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.
Your answer
Follow this Question
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