- Home /
Shooting with raycasting or physics?
Hello. I am curremtly making a game that needs realistic shooting. Is it better to do this with raycasting or rigidbody physics attached to a bullet object. I have heard in places that rigidbodied bullets can go through things but they are more realistic. I would prefer to use rigidbody physics as well because I have knowledge on them. Is the rigidbody going through objects a problem and if it is how can I solve it? Thanks, drift501
so, you guys are saying, to make a realistic shooting, has to be an rigidbody and a raycast from the rigidbody?
It depends what you want to simulate. For example Quake3 (or QuakeLive) has a lot of weapons. Some are instant hit weapons (machine gun, railgun, shotgun, lightning), some are projectile weapons (all others like rocket launcher, plasma, grenade).
Instant hit weapons are always using a raycast. Projectile-weapons use a real object that moves from the shooter towards the target.
In any case it's recommended to use a Raycast, but in a different way. Instant hit weapons cast a ray with a length of your weapons max range value. projectile weapons only cast a ray ahead their next movement step. See DontGoThroughThings
Btw. There are cases where you might think it's a projectile, but it isn't ;) In Unreal (the old one) the shockrifle's primary attack looks like a projectile, but it is an instant hit shot. It's just an animation.
Answer by aldonaletto · May 20, 2012 at 01:18 PM
You can reasonably simulate a real shot with raycasting: do a first exploratory raycast when the fire button is pressed; based on the hit.distance, calculate the drop and flight time, wait for the flight time delay and do the actual raycast shot taking the drop into account - like this:
var bulletSpeed: float = 1000; // bullet speed in meters/second var shotSound: AudioClip;
function Fire(){ if (audio) audio.PlayOneShot(shotSound); // the sound plays immediately var hit: RaycastHit; // do the exploratory raycast first: if (Physics.Raycast(transform.position, transform.forward, hit)){ var delay = hit.distance / bulletSpeed; // calculate the flight time var hitPt = hit.point; hitPt.y -= delay * 9.8; // calculate the bullet drop at the target var dir = hitPt - transform.position; // use this to modify the shot direction yield WaitForSeconds(delay); // wait for the flight time // then do the actual shooting: if (Physics.Raycast(transform.position, dir, hit){ // do here the usual job when something is hit: // apply damage, instantiate particles etc. } }
Will probably work in 95% of the cases. The only problem I can see is if you have an obstacle in the way that would have been passed (e.g. not hit) on a ballistic flight path while the dropped ray will falsely return a hit. But I suppose that's why you wrote "reasonably" ;)
I think that you can choose a group of layers that your raycast can "hit". Otherwise you could check if the object is passable, apply some "passing effect" and call the same raycast-bullet function in the same direction of the original-one, with the origin inside the passed object.
@hathol, yes, that's what I mean by "reasonably": it will hit walls that could be passed in a real ballistic trajectory, and have two other possible problems: if the target moves out during the flight time, a farther object may be hit with the wrong delay and drop; and if another closer object occludes the target during flight time, again the drop and delay will be incorrect.
The rigidbody+raycast alternative you've proposed is the only real "bullet-proof" approach: you can use backward or forward raycasts each FixedUpdate, with bulletSpeed * Time.fixedDeltaTime as the distance (the distance traveled between physics cycles).
Answer by hathol · May 20, 2012 at 11:58 AM
You should separate the logic from the visual representation. Use raycasting for the actual hit detection since it's more reliable. Then you can use actual bullet objects (even with rigidbody physics if you like) or any other form of visual representation of the shot (in my current project we are using a particle emitter for a machine gun for example).
Can I simulate bullet drop and flight time with raycasting?
Depends on the level of accuracy you need. You can always change the angle of the ray slightly downwards or delay the casting. But if you're making a sniper simulation where you really need an accurate flight path, then no. But if you're using physics simulation and want to be sure that a bullet will not accidentaly go through an object, you could still use raycasting to double check your results. Basically fire a (very short) ray from the bullet towards it's current backward direction to make sure you didn't accidentally pass something you shouldn't have.
Your answer
Follow this Question
Related Questions
Adding real life physics behavior on 3-d object. 1 Answer
Applying proper drag and center of mass for a vehicle 1 Answer
how to create the magnet similar to the real magnet ? 1 Answer
Calculate Distance using WheelColliders 3 Answers
Add force to Rigidbody towards clicked direction using Raycast 0 Answers