- Home /
OnTriggerEnter to happen after Rigidbody has stopped moving?
I am a beginner/ intermediate coder. I making a little angry birds style, fling the object game, but more like golf. My issue is that i want my flung object to land on the target and only get a point if the object has stopped moving within the target bounds.
I have the flung object (cube) with a rigidbody, and the target with a collider set as trigger. OnTriggerEnter works just fine in detecting that the cube is on the target, but if i use Rigidbody.isSleeping in any part on the OnTriggerEnter event it doesnt work. I am assuming because it is "awake" if its in another collider.
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Target")
{
// Works
Debug.Log("in trigger");
// Doesnt Work
if (rb.IsSleeping())
{
Debug.Log("Point!");
}
}
}
I tried checking the Rigidbody's velocity as well, still didnt work.
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Target")
{
// Works
Debug.Log("in trigger");
// Doesnt Work
if (rb.velocity.magnitude < 0.1)
{
Debug.Log("Point!");
}
}
}
I also tried nesting the collider check in the velocity/isSleeping check instead and still nothing.
Maybe there is a better way entirely to do this. Note: I dont think i can do a distance check because maybe the object will be just under the target or something. Also not to familliar with raycasting, maybe it is an option, the object will rotate and tumble because its a rigidbody so i dont know if that messes with raycasting down.
Answer by jeffstaples1982 · Jul 29, 2021 at 04:29 AM
Instead of using the velocity magnitude have you tried just using velocity? I have very limited knowledge since I've only been using Unity for a few months and mostly work on UI things so I don't know if this would work.
if(rb.velocity == Vector3.zero)
{
//Do stuff
}