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?