- Home /
Bullet goes through object
I have a "bullet" (just a little cube). It's got all the colliders and everything. Whenever I shoot it to a box in-game, which has a box collider and rigidbody, the bullet goes straight through it. Only occasionally does the box fall back as it is hit. Help?
Answer by wibble82 · Mar 11, 2014 at 05:30 PM
Physics engines like the one in unity operate by default in 'discrete' mode. This means that each frame it looks at the position of each object and checks if any are overlapping. If they are, a collision occured. It does it's stuff, then moves each object forwards by its velocity.
In your case I would imagine your bullets are moving very fast, so its entirely possible they manage to be on 1 side of the target on 1 frame, but pass right through to the other side in a single time step (cos their velocity is so high), so end up on the other side of the target without ever having overlapped it.
The simplest but most computationally expensive method of fixing this is to use continuous collision detection as documented here: https://docs.unity3d.com/Documentation/ScriptReference/Rigidbody-collisionDetectionMode.html
However, for bullets I often use a much simpler approach. If your bullets are very small and are moving in a straight line, you don't really need them to be full rigid bodies. Instead, just make them not collidable (they don't even need a collider or a rigid body). Store inside them a 'move speed' and each frame:
calculate the position you'll be in next (transform.position+move_speed*Time.fixedDeltaTime)
do a ray cast from current position to next position
if the ray cast hits something, your bullet has collided and do stuff!
if the ray cast doesn't hit something, move your bullet to the next position
Just like with collisions you can choose to ignore certain results and use layers to filter your ray cast.
That's the standard 'cheap' way to do bullets, as it avoids doing complicated physics. You're ultimately just treating the bullet as a fast moving point, rather than a body with volume. Since bullets are usually pretty small there's no noticeable effect.
I don't have code to hand which is a shame cos I've got a game at home doing just this. Ray casts are well documented though so a few google searches should tell you what you need to know.
Note: This MUST be done inside a FixedUpdate function, not an Update function, as it runs in tandem with the physics and you might see some weirdness if you just leave it in Update.
Extra note: If your bullets are moving REALLY fast, and you want to save a little performance, you can often just do a ray cast when they are first fired, get the hit position then move them to that point over a few frames. Technically if the target moves the result is no longer correct but for objects that reach their target in a fraction of a second this is rarely noticeable. Handy for things like machine guns where you'll have lots of bullets flying around at once. Most shooters use this approach - what/whether you hit something is determined the moment you click fire, and the moving bullet is just a graphical effect.
Extra extra note!: And if you want your bullets to still have a physical effect on your target, check out AddForceAtPoint. You can calculate where your bullet hit the target, then apply a small force there so it gets pushed around when hit by a mailstrom of metal!
I would go for Linecast ins$$anonymous$$d of Raycast.
Vector3 prev = Vector3.zero;
void FixedUpdate(){
if(prev != Vector3.zero){
RaycastHit hit;
if(Physics.Linecast(prev,transform.position, out hit)){
// Coliision happened at hit.point
}
prev = transform.positoin
}
}
By using this method, I can't reference the object with other.gameObject. How could I achieve the same procedure with this method?
the collided object is stored in the RaycastHit struct.
hit.collider.gameObject;
Yeah line cast is better - didn't realise that was in there. I'd still do away with rigid bodies and colliders altogether though. Unnecessary complication unless you need the bullets to bounce around and rotate.
To move bullet do not use transform, use rigidbody methods only.
Answer by hidro636 · Mar 11, 2014 at 05:48 PM
My guess is that the velocity of the bullet is putting it on the other side of the collider instead of actually colliding with it. For example, if the "bullet" is moving at 10 units per frame and the object that it's aimed at is only 5 units long, the cube is going to "jump" past the collider depending on the distance it was fired from.
Not entirely sure what a good fix would be except for possibly casting a ray from the rear of the bullet and checking to see if that ray is colliding with the object it was supposed to hit.
Hope that helps a bit
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Bullet Sometimes Go Throw Object 0 Answers
Bullet Hole not inline with sight?(Center of Screen) 1 Answer
dont instantiate clones 1 Answer
Hinge Joint goes crazy 1 Answer