- Home /
Rigidbody2D.IsSleeping issue
When checking the velocity of an object with ...velocity.x == 0 && velocity.y == 0 everything works fine but if i use Rigidbody2D.IsSleeping there seems to be a small delay?
if (rb.IsSleeping)
{
// Calls after the rigidbody has stopped moving +0.2 - 0.3s
}
if (rb.velocity.x == 0 && rb.velocity.y == 0)
{
// Everything works fine but for reasons i can't use this
}
Answer by FortisVenaliter · Apr 21, 2017 at 03:53 PM
Yes, since it's a physics object, it's possible that while it's not moving right now, it has forces acting upon it that will make it start moving soon. The "isSleeping" parameter refers to whether the object is "at rest" in a physics sense, meaning that all forces are countered and the velocity is zero. To verify this is the case, it needs that extra bit of time to settle down.
I was hoping to fix that but okay since that is the case, my problem is I'm checking if the player isn't moving and then checking for input, when I use IsSleeping everything works fine except for the delay, but if i use the velocity check the player can press multiple buttons and I can't find a workaround :/
Sounds like you just need to if/else the input code so that only one button press is recognized on a single frame. Or add a queuing system for button presses.
I actually do have a queue system and for some reason it still happens..,
List<$$anonymous$$eyCode> queued$$anonymous$$ovement = new List<$$anonymous$$eyCode>();
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.UpArrow) && queued$$anonymous$$ovement.Count < 2)
{
queued$$anonymous$$ovement.Add($$anonymous$$eyCode.UpArrow);
}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.RightArrow) && queued$$anonymous$$ovement.Count < 2)
{
queued$$anonymous$$ovement.Add($$anonymous$$eyCode.RightArrow);
}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.DownArrow) && queued$$anonymous$$ovement.Count < 2)
{
queued$$anonymous$$ovement.Add($$anonymous$$eyCode.DownArrow);
}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftArrow) && queued$$anonymous$$ovement.Count < 2)
{
queued$$anonymous$$ovement.Add($$anonymous$$eyCode.LeftArrow);
}
if (rb.velocity.x == 0 && rb.velocity.y == 0 && queued$$anonymous$$ovement.Count != 0)
{
switch (queued$$anonymous$$ovement[0])
{
case $$anonymous$$eyCode.UpArrow:
if (raycastHit[0].collider == null)
{
$$anonymous$$ove(0);
}
break;
case $$anonymous$$eyCode.RightArrow:
if (raycastHit[1].collider == null)
{
$$anonymous$$ove(1);
}
break;
case $$anonymous$$eyCode.DownArrow:
if (raycastHit[2].collider == null)
{
$$anonymous$$ove(2);
}
break;
case $$anonymous$$eyCode.LeftArrow:
if (raycastHit[3].collider == null)
{
$$anonymous$$ove(3);
}
break;
}
queued$$anonymous$$ovement.RemoveAt(0);
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
C# Non-Static Member Rigidbody2D.MovePosition 1 Answer