Bullet Raycast Help?

I have been searching the interweb(and BA resources section) for a while now, and I found very few tutorials about how to do rayCast bullets. :spin: The ones I did find had broken links and were outdated. :mad: I thought this would be a commonly requested thing. So, has anyone found a nice up to date tutorial? :confused:


I don’t really know of one, but it shouldn’t be too difficult, right? Are you looking for something like bullet holes or pushing objects around when they’re fired at, or just a simple ‘get the object I shoot and deduct health’ piece of code?

Goran made this pretty good tutorial on how to use the ray sensor.

@SolarLune, mainly damage, but bullet holes would be a bonus. Don’t you use influence for pushing objects?

@Mobious, thanks! Goran is great. :smiley:

Well, if a object is made of many smaller sub objects, and loosing enough health would make it disappear,

think mine-craft with way higher resolution and instead of a “pick” you have a gun, that makes blocks disappear,

So on collision with bullet end object, if the object is physical you could have everything linked with 6dof joints and rigid body, or all of the pieces around the piece you shot at least… then as they swing to there max distance from the pinning point, they could let go, so little pieces could fall out like they were snapping off from there own weights, or some could stay attached, this would create rough random holes like blowing a hole in particle board,

so I think making a piece of wall and doing fracture, and then using all the pieces to make the object would be interesting, and hard…
it would also be hard to make it look like it was not cracked before it was struck…

A physical bullet can do all of this as well, instead of casting a ray, have
add object with a high velocity, and low mass, this will impart momentum and produce a collision so
property health = 100
Collision w/property bullet-------And---------property Health Add (-5)

if health < 0 -----------and ----------end object

here is a old game I was making to learn mouse + weapons etc

it is from a long time ago, and has a lot of newbishness, so be gentle… but it has bullets + damage + explosions + mouse-look and a Gatling gun :slight_smile:



BroBotsMouse (2).blend (1.77 MB)

@BluePrintRandom - The reason raycasts are generally done for bullets is because the physics engine doesn’t deal with very small objects well at all. If you use small physics-enabled bullets, they can slip through objects and go places they shouldn’t. With raycasts, they’re efficient (enough, at least) and accurate.

@cam.dudes - Yeah, I did. I don’t usually do FPS or TPS games, so I have limited experience with it, though. Something like this should work, though:

impulse = -ray[2]#rayhit.worldPosition - obj.worldPosition

impulse.magnitude = 0.5

rayobj.applyImpulse([0, 0, 0], impulse )

Ray[2] is the third argument of a variable containing the result of a rayCast() function (the returned hit normal). With this, the object would get a pretty slight impulse in the opposite of the hit normal. If that doesn’t give you the result you’re looking for, you could also try rayhit.worldPosition - obj.worldPosition, as it’s commented out above. That would push the object in the direction of where you shot to where the object is. The most accurate would be to imply a forward vector impulse at the point where the bullet hits (which is set to [0, 0, 0] above), I think. Check the applyImpulse() API call for more info.

yeah, but a missile, is hard to represent with a ray, and I can have a collider longer then the bullets visible dimensions, but only accept 1 collision by “ending” the bullet on the first collision,

I can’t have a artillery shell look right if I don’t know where it is…
a ray is instant as well, which is not how real bullets work,
they drop, the wind moves them etc…

There is an FPS template for 2.6 that includes the scripts for it, both impulse and bullet hole.

I haven’t looked over those exact scripts, was using it for movement python learning, but you can download it here:

I just made a missile that uses 3 vision cones, and messages and forces, and no python or track to, and hits a moving target :slight_smile:

A missile would be better represented by a physical game object, but a ray is best for bullets. Also, a ray is only as instant as you need it to be. The mechanic of bullets dropping and being moved by the wind isn’t done by a physical bullet and the physics engine. I would assume that in ‘real’ commercial games, they still use ray casts, but in succession (i.e. a ray that goes only 50 Blender Units each frame, and so on until a hit or it’s too far) to have ‘dynamics’ applied to the bullets. Each ray gets the appropriate dynamic applied to it. That’s how I would do it in the BGE, as well.

this one i found is very useful!!