- Home /
Is there any way to increase a triggers check rate?
I tried using a trigger to see when a ball entered an area. Everything worked as long as the ball moved at a low speed. Although when the ball was moving at a high speed the trigger would not detect the ball even though the ball had passed through the trigger. Is there any way to increase the check rate of the trigger or prevent triggers from missing objects moving through at high speeds?
I'm currently using rigidbod2D.velocity to move the ball.
I threw together a quick project to showcase the problem. The blocks on the sides are triggers that inverse and increase the balls x velocity. The ball script...
public class Ball : MonoBehaviour {
Rigidbody2D rb2d;
// Use this for initialization
void Start () {
rb2d = GetComponent<Rigidbody2D>();
rb2d.velocity = new Vector2(6, 0);
}
}
The block script...
public class Block : MonoBehaviour {
private void OnTriggerEnter2D(Collider2D collision)
{
GameObject obj = collision.gameObject;
if(obj.CompareTag("Ball"))
{
Rigidbody2D rb = collision.GetComponent<Rigidbody2D>();
rb.velocity = new Vector2(-rb.velocity.x * 1.1f, rb.velocity.y);
}
}
}
How can I prevent the ball from flying threw the trigger?
Answer by IvovdMarel · Aug 06, 2018 at 02:04 AM
Trigger checking is NOT related to FPS, as it runs in the FixedUpdate, as opposed to the Update.
https://docs.unity3d.com/560/Documentation/Manual/ExecutionOrder.html
FixedUpdate (and Physics) by default runs every 0.02 second. In Edit > Project Settings > Time you can adjust your Fixed Timestep.
Alternatively, try changing the interpolation/extrapolation options in the Rigidbody (see docs for more details)
Lowering the timestep definitely uppers the triggers checkrate. Interpolation and extrapolation does not seem to have any effect on the ball when it comes to preventing the ball from flying past the blocks in my example. By changing the timestep I was able to keep the ball from flying through the triggers for longer but when the ball reached a higher speed it still flew past the triggers like in the example.
Is there any good way of completely preventing the ball from passing the triggers unnoticed?
Yes there is. Just tell your object: DontGoThroughThings ^^.
This script is already 11 years old. Discrete time step simulation would always have the problem at a large enough speed it would penetrate through any colliders. This is actually similar to the Wagon-wheel effect.
The solution in the script is to actually perform a raycast along the distance the object would travel between two time steps.
Thanks that's cool. But I noticed that if I switch from using triggers to colliders and set the rigidbodies collision detection to continuous that the ball will never pass the collider (and I think always trigger the onCollisionEnter) no matter the speed, is there no such option for triggers?
Answer by SamohtVII · Aug 06, 2018 at 01:55 AM
The issue would be the FPS would it not. As the ball moves at each frame it moves bit by bit. At, for example, 30 fps over 30m distance it does 1m/frame. At 60m it goes 2m/frame and so on. If it hits (For example) 4m at frame 4 and 6m at frame 5 and the collider is at the 5m mark it will not detect it. I know this isn't an answer but maybe with that knowledge a solution can be found. Possibly checking the x and y each frame and comparing that with the collider x and y and seeing if it would have passed through.