Instant bullet?


So I’m trying to make a simple first person shooter, but am having some trouble. Acctually just one main problem for now. How would you go about making an instant bullet type thing? Like the bullet hits right in the center of the crosshair as soon as you click.


Something python related I’m guessing

Ya me too. I looked at the fps template, but I cant figure it out.

In the bullet template, and most fps (blender) games, a ray sensor is used. The ray emits from an empty (at the tip of the gun), and (in python) returns the object it hits and the location. The python script then uses the applyimpulse function to move the hit object, or sends a message to the hit player/ai.

Ok, but I really have no clue when it comes to python.

Check my sig, it’s made by either placing a cube at a ray’s hit position or applying force on that direction.

So then how exactly do rays work? Are they just true or false if something is in front of them? Or what?

read up on python tutorials, it’ll explain all you need to know about coding in the BGE.

Basically, a ray is a line that starts at the center of the object casting it, and goes until it hits something, or hits the maximum range. If it hits an object, and the object has whatever property the ray is looking for, it returns true - otherwise it returns false. If it hits an object, it also returns where it hit, the object’s name, and the normal of the face it hit.

sorry my explanation is so bad -_-;

Ok so basically when you click check if the ray is true.

Get the ray sensor:
ray = cont.getSensor(“therayname”)
It’s better to avoid errors using if ray.isPositive():
Simple XD

Ok thanks, so then one last question, what would I put for point and impulse for applyImpulse?

the point should be the position the ray hits minus the position of the object it hits, and the impulse should be the direction the ray is cast along times how strong you want it to be.
this thread will either enlighten or confuse you (hopefully the former)- it assumes some knowledge of the bge and python, but since you’re using applyImpulse hopefully you’re at the right level.

Ok so then another thought I had, how would I make it so that when you click it shoots an object at the point the ray hits? This way I could use soft bodies. Just because it would be fun.

By the way, thanks for the tutorial.It works pretty good, just one problem. When I move the crosshair over an object it just goes shooting off like I shot it.

Well, you could either shoot the softbody in the direction of the cursor fast, hoping it’ll hit pretty close, or you could use the ballistics equation (I don’t remember the actual name of the equation, nor the equation itself, sorry) to find the exact angle to shoot it at so it hits right. Obviously the latter is a lot more work, but it’s the only way I know of to get it completely right. Of course, I could easily be overlooking some very simple and obvious answer, that happens a lot.

…In regards to the tutorial, maybe the mouse sensor is misfiring? I could take a look at the .blend if you want.

Well I’m thinking more of shooting just a spere of something towards the ray end, so that soft bodies would work when you shoot them.

oh, I see what you mean. What social did in his fps-thingy was similar to mine, except he spawned an object at the collision location which moved in the direction of the ray, then deleted itself the next frame. Sounds like that might be a better option for you in this case.

Well ok but I’m kinda thinking more like metroid or something where you acctually see the bullet come from the gun and move towards the target. So basically I need to know how to make an object move from the gun to the end of the ray.

that would make the bullet non-instantaneous, but much simpler; have the gun spawn a bullet object (which doesn’t have physics, or else it will curve down) the bullet moves forward at some speed (make sure the length of the bullet is more than the distance it will go in a frame, or it could go through walls)
use logic so when the bullet hits something it does some damage to the hit object then deletes itself. The only python you need is to get the name of the hit object, and deal damage to it, the rest can easily be bricks. (no ray necessary)

Ok I tried that, but it doesnt always hit where the crosshair is aiming. So I’m thinking of setting the bullet to move to where the ray hit when it’s created.