- Home /
Projectiles too fast for Physics
hey guys,
I'm having a problem with projectile physics.
The projectiles in my game are very fast, so fast in fact, that OnTriggerEnter events, are recognized too late sometimes, and the bullet is technically able to fly through a thin wall.
Heres an example code:
void OnTriggerEnter(Collider other)
{
if (COLLIDER == WALL)
{
DESPAWN PROJECTILE
}
}
is there any way to speed up the collision detection with OnTriggerEnter. So that the collision calls are framerate independent?
The projectile is moved like this btw. (2D game):
transform.Translate(Vector3.up * Time.deltaTime * speed);
I thank you very much.
Answer by Graham-Dunnett · Nov 12, 2012 at 03:23 PM
Perform a ray cast from the position the projectile is now, to where it'll be in the next frame. If the ray hits anything, then so will the projectile. You can use the distance along this ray where the collision occurs to tell you when the bullet would hit.
Answer by Owen-Reynolds · Nov 12, 2012 at 03:56 PM
Can look at / read the CollisionDetection options in the rigidbody, to force it to check "in between" frames.
But, if it's going that fast, it may not look good anyway, and the raycast method may be better.
Answer by Dede24 · Nov 12, 2012 at 05:07 PM
I found a more simple approach, which seems to get the job done. I basically just made the OnTriggerEnter event a coroutine, which works with Fixed Update:
IEnumerator OnTriggerEnter(Collider other)
{
yield return new WaitForFixedUpdate();
if (WALL)
{
DESPAWN
}
}
on top of that, I reduced the Fixed Timestep in the Timemanager to 0.005. Now no bullet flies through a thin object anymore.
But I might go back to the raycast method, if this approach doesnt work over the long run.
But thanks to all of you, for the suggestions.
Doubt the coroutine does anything extra -- gotta be just the change in fixedTimeStep.
The physics system is already checking OnTrigger during each fixedUpdate.
Your answer
Follow this Question
Related Questions
Drag & Drop GameObject with MovePosition shoots other Gameobject away in case of them colliding 1 Answer
Make Ray hit own collider 1 Answer
How to make physics collision dotted simulation like bubble shooter ? 2 Answers
Disable inertia tensor calculations on rigidbody? 0 Answers
Collision Query 1 Answer