Raycast script for bullet?

Is there a script for raycasting for bullets here? I couldn’t seem to create on myself with logic bricks and the little python I know…

I just want something to eliminate the fast moving bullet that causes errors. If the bullet moves too fast (at regular bullet speeds) then the collisions probably won’t happen…If I make the bullet too slow however, then it will be unrealistic

I’ve been told that if I just use the bullet object as a dummy and shoot a ray out of the empty shooter object, then that will eliminate the errors…

If there’s any other way to make the game bullet more realistic and error-free (or at least as close as possible), that would be appreciated

thanks

In pro games, there are no bullets. Just bullet holes:

##############################################################
#Blender 2.5 bullethole script

#this script is free to use and is under no license agreement.

##############################################################

from bge import logic as GameLogic
from mathutils import Vector
c = GameLogic.getCurrentController()
own = c.owner

space = 0.003

sensors

mouseclick = c.sensors[“mouseclick”]
ray = c.sensors[“ray”]

actuator

spawn = c.actuators[“spawn”]

if mouseclick.positive and ray.positive:

Get info

pos_vec = Vector(ray.hitPosition)
normal_vec = Vector(ray.hitNormal)

make object

spawn.instantAddObject()
bullet_hole = spawn.objectLastCreated

position hole

bullet_hole.alignAxisToVect(normal_vec.xyz, 2, 1)
normal_vec.magnitude = space
bullet_hole.worldPosition = (pos_vec + normal_vec).xyz

here is a demo I have been working on

if has

Zoom

Shoot

Ammo

Stock-Ammo

Gun swing

and walking.

Attachments

FPSSetupX.blend (695 KB)

I make this example a some time ago, have a simple bullet hole and enemy health system.
The ray is casted using a sensor.
fps using rays v0.2.blend (560 KB)

This is not true whatsoever. “Pro games” as you say, such as Arma 2 (dayz,) and battlefield, take into account bullet velocity and bullet drop (weight.)

Let say you’re shooting a sniper rifle that fires a 308 round, with an average velocity of 800 meters per second, at a moving target 200 meters away. That would take 1/4 of a second for the bullet to reach the target. And if the target is running 6 meters per second, then you would need to aim 1.5 meters, or nearly 5 feet, ahead of the target to hit it, and only if it keeps moving.
Most professional games would take this into account.

PluePrintRandom, when you post files, I suggest naming things so others can see what’s going on. You have 4 python scripts all named the same. Maybe you know what’s going on, but in a year or two you’ll wish you named everything so it makes sense.
And maybe look into using the expression controller, so you don’t need both a positive and negative keyboard sensor.

Nice, but it is still an instant straight hit, no matter how far the target is.

The reason there are errors is because of the games frame-rate. In a game running at 60fps with a realistic bullet moving 600m/s, the bullet will be moving 10 meters every frame, and miss anything in those 10 meters.
So if you cast a ray the distance the bullet moves each frame, you will have error free collision, and real bullet speed.

With just a little bit of math, you can have real bullet speeds, and error free collision detection.

  • Have a scale for your scene, lets say 1 blender unit = 1 meter
  • Set the bullet speed. For example, an M1911 at 250 m/s
BULLET_SPEED = 250
  • Get the frame-rate.
fps = bge.logic.getLogicTicRate()
  • Get the distance the bullet needs to travel each frame. Divide the bullet speed by the frame-rate. If the game is running at 30 frames per second, then the bullet needs to travel 8.33 meters every frame to keep its speed, or 4.16 meters at 60fps.

distance = BULLET_SPEED/fps
  • Apply movement to the bullet.
bullet = bge.logic.getCurrentController().owner
bullet.applyMovement((0,distance,0), 1)
  • Get the position the bullet will be at in the next frame. Get the Vector the bullet is traveling, multiply it by the distance, and then add it to the bullet’s current position to get the new position the bullet will be in the next frame.
from mathutils import Vector

y_vect = Vector((bullet.orientation[1]))
y_vect.magnitude = distance

endpoint_vect = bullet.worldPosition + y_vect
  • Cast a ray. What you do next is up to you. You can apply force, add bullet holes, sound, and sparks.
hitObj, hitPos,  hitNorm = bullet.rayCast(endpoint_vect, bullet, 0, "", 0, 0, 0)
  • Add bullet holes, sparks, sound, apply force, or add damage to hit object.
if hitObj != None:
    if 'wood_wall' in hitObj:
        #add wood bullet hole
        bullet.endObject()

    else:
        bullet.endObject()

If anyone would be interested in a more complete demo file or template let me know.

Want to help me make a super genaric gun rig?

I want to have all logic in gun for firing the gun, and all reload logic, but use the player t trigger firing and reloading, I have a add object bullet set, and fire animation but I did not add raycast etc yet.

Check Open Project Wrectified

This is not true whatsoever. “Pro games” as you say, such as Arma 2 (dayz,) and battlefield, take into account bullet velocity and bullet drop (weight.)

This is definitely true. So what I tried to do was use the bullet (dynamic) and add a limited ray sensor that the bullet. The ray covers the bullet and the distance it “jumps” That way, the bullet can travel at its natural speed and not have any problems…or so I though. in close situations, the bullet never even appears

…make a super genaric gun rig?

Can’t I have multiple guns for the game…pistol, auto, snipers that all have different holding positions generic would just make everything a little less suiting because you don’t want a pistol and a sniper to share the same position as an auto rifle

If anyone would be interested in a more complete demo file or template let me know.

yes please. while reading and understanding is cool, nothing is more complete than a simple demo

Nice, but it is still an instant straight hit, no matter how far the target is.

for some games, this is OK because it might be in small confined spaces

I just want to know if my idea would work…
So the bullet is spawned and shoots an angled array(downward) to where the bullet will be next frame. The bullet goes to where the ray was last frame and deletes if there is a collision, sending the collision to the object it collided with. the collided object can then adjust accordingly

Ok Thanks, good to know that whatever you think of has already been done before

This is not true whatsoever. “Pro games” as you say, such as Arma 2 (dayz,) and battlefield, take into account bullet velocity and bullet drop (weight.)

That does not mean there are “physical” bullets. If 10 guys are shooting with automatic weapons real bullets (let’s say 10000 bullets) with the physics dynamic at the same time, your game will run very slow…