6
$\begingroup$

I've been researching this for weeks and only found bad results. I think its time for a good answer. I have a rocket launcher, so I can't "fake" the bullet. There is no way to fake a rocket. I need it to shoot from the front of the gun and fly to the center of the screen. Of course I could just make the weapon slanted to the center, but if you look at far targets, it will miss; and if you look at close targets it will also miss. Can I have an empty in the center of the screen that just collides with obstacles, and the gun just adds the projectile to make it always hit center? If that's the case, I would kindly ask for help on how to do that. If there is a better technique, please state it, it would help a lot!

-People said they are confused, let me explain more thoroughly.

I want to have a line in front of the camera that collides with obstacles(for example, and object with property "obstacle" set to 1), that is stopping at infinity, but there is will obviously always be an obstacle to hit, such as a roof if you look up(indoor). The empty will always track to the very end of the line, so if you aim somewhere close, the rocket will hit center of screen and hit the close object, and if you hit far, on the other hand, the rocket will fly to center of screen and hit the obstacle still. So you will basically always hit the center of the screen no matter what.

$\endgroup$
4
  • $\begingroup$ Do you want to use python...? or another way. $\endgroup$ Jul 6, 2014 at 1:34
  • $\begingroup$ Anything, I just want the bullet to hit the center. $\endgroup$
    – blackhole
    Jul 6, 2014 at 23:33
  • $\begingroup$ Sorry, could you explain better? What do you want to do exactly? $\endgroup$ Jul 9, 2014 at 18:27
  • $\begingroup$ the problem we all have is the following: a rocket will fly a straight line (+ gravity, if you want so). The "center of the screen" is a straight line, too (3dimensionally speaking). So "hitting the center of the screen" is not a point but a line, which means we have to select which point. Please be clear which point you mean there (maybe you mean infinity) $\endgroup$ Jul 9, 2014 at 20:44

4 Answers 4

5
$\begingroup$
import bge

cont = bge.logic.getCurrentController()
scene = bge.logic.getCurrentScene()

target = scene.objects['target']
ray = cont.sensors['Ray']

if ray.hitObject and 'bullet' not in ray.hitObject:
    target.worldPosition = ray.hitPosition

This script will move a empty (named target) to wherever the Ray sensors hits a another object. The gun has a Track to Actuator which makes the gun always point to the target empty. I have the mouse look actuator set up on the camera to make the camera follow the mouse, all the gun parts are parented to the camera so they move as well.

Demo file
Mouse makes the camera look around space to fire.
Notice how the gun will sightly move depending on whether the target is close or far away.

$\endgroup$
2
  • $\begingroup$ Thank you so much! People did not have this question answered in blender for a very long time. Here it is! :) $\endgroup$
    – blackhole
    Jul 22, 2014 at 6:35
  • $\begingroup$ @BlenderDev just too bad you did not come online with in the time for the bounty. $\endgroup$
    – David
    Jul 22, 2014 at 12:56
6
+25
$\begingroup$

You can use a bit of Python to get started, without knowing your exact implementation of the weapon system, i can't give you the full code.

# get vector from centre of screen
cam = logic.getCurrentScene().active_camera
# vector in world space
vect = cam.getScreenVect(0.5, 0.5)
# get length of vect
length = -cam.position[2]/vect[2]
p = vect * length
# position of p (somewhat ahead of camera)
pos = cam.position + p

With that, you should be able to either ray cast the bullet, or have it go toward that direction.

$\endgroup$
0
2
$\begingroup$

This is a nice, simple way I do it for FPS style games in Blender:

Parent an empty to the camera (move it to the camera position using Shift S and move it in front of the cam). This is your 'gun fire' object.

On another layer, create a bullet object. On your gun or character, add an integer property called ammo if required.

On your gun empty, add an Always sensor, Ray sensor and Mouse (or keyboard) sensor for firing. Set the ray distance and direction. Leave the Property field blank. Add a python script brick.

The Ray sensor has a number of attributes you can use, namely hitObject, hitPosition, and hitNormal

This very simple script will deplete ammo etc when the fire button is pushed and will place a projectile object at the hitObject, IF there is an object coliding with the Ray.

import bge


def main():

    cont = bge.logic.getCurrentController()
    own = cont.owner
    scene = bge.logic.getCurrentScene()

    ray = cont.sensors['Ray']
    fire = cont.sensors['Mouse'] 

    hitobj = ray.hitObject
    hitpos = ray.hitPosition

    if fire.positive: #is the fire key pressed
        own ['ammo'] -= 1 #reduce ammo by 1
        if (hitobj): #IS THERE SOMETHING COLLIDING WITH THE RAY?
             bullet = scene.addObject ("bullet", hitobj, 3) #place object called bullet at the center of the hitobject
             bullet.worldPosition = hitpos #place bullet at the exact point the ray hits the hitobject



main()
$\endgroup$
2
  • $\begingroup$ Its not working, sorry. Can you please put a .blend from a website like pasteall. Im confused, I followed everything you said. Also, im new to python so I cant really understand it, but it seems that you are adding the bullet to the end of the ray, I want the bullet to fly from the gun to the end of the ray. The ray sensor, is there any way you can make it infinity? $\endgroup$
    – blackhole
    Jul 10, 2014 at 23:37
  • $\begingroup$ This script does the exact opposite of what the questioner wants. He explicitly said that he can not fake the rockets, and that is what your script does. $\endgroup$
    – David
    Jul 16, 2014 at 0:49
-1
$\begingroup$

If your making an fps game, just make sure the gun (etc) is pointing directly in front of you. It automatically travels to the centre of the screen and keeps going forward.

$\endgroup$
1
  • $\begingroup$ No? It will go forward but be on the right. $\endgroup$
    – blackhole
    Jul 22, 2014 at 6:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .