- Home /
Outdated
rigidbody2D velocity zero?
I have two scripts, in one of the scripts i set the rigidbody velocity of the object it belongs to, using fixedupdate, like so:
void FixedUpdate()
{
if (Input.GetMouseButton(0))
{
vVelocity += vSensitivity * Time.deltaTime;
}
else
{
vVelocity -= vSensitivity * Time.deltaTime;
}
Vector2 velocity = new Vector2(hSpeed, vVelocity);
transform.right = velocity;
rigidbody2D.velocity = velocity;
}
and the other script, which uses the velocity of the rigidbody, referenced through a transform:
void FixedUpdate()
{
if(followTransform.rigidbody2D != null){
Debug.Log("Has rigidbody with v: "+followTransform.rigidbody2D.velocity);
}
}
This just outputs "Has rigidbody with v: (0, 0)", and sometimes (once out of 100 times) gives out the correct value, although the object is clearly moving and the transform is never directly changed.
If i set this second script's execution order to be sooner than the default, everything is working as expected.
Why is this happening and how can i solve it without modifying the execution order?
Have you tried:
rigidbody2D.velocity.sqr$$anonymous$$agnitude;
That always works for me.
I think that if the velocity itself is a (0, 0), whichever function i call operates on those values.
@highpockets sqr$$anonymous$$agnitude
has nothing to do with the question.
I realize it doesn't directly answer the question, but it gives an alternative to find out the speed of the rigidbody. Correct me if I'm wrong, but that seems to be the goal here. I don't know if it will solve the problem, but sqr$$anonymous$$agnitude has given me good results when I need to find the speed of a rigidbody. Therefore, I think sqr$$anonymous$$agnitude does in fact have relevance here BP1.
it doesn't, because magnitude is the length of the velocity vector, so is a float value. Sqr magnitude is this length multiplied by itself which is even further away from what the user wants.
$$anonymous$$aybe put the debug.log bit in update rather than fixed update?
Follow this Question
Related Questions
NullReferenceException: UnityEngine.Rigidbody2D.get_velocity() 0 Answers
Character won't move (Fixed) 1 Answer
Velocity from Rigidbody and from Transform don't match for small dt 1 Answer
Convert from m/s to m/fixedFrame 1 Answer
Do rigidbodies (especially 2d) have limits on their velocities? 1 Answer