# raycast/bullet force

I'm currently developing a game and I'm in need of the bullet force script. I was following a YouTube channel on how to do it but they are quite fast and I think I had a mistake in the code somewhere. Please help Here's the code

Import bge
from bge import render
render.show mouse[1]

def main() :
cont = bge.logic.getCurrentController()
own = cont.owner

force = cont.sensor ["ray"]
mouse = cont.sensors ["mouse"]

if ray.positive:
render.drawline[own.world position, ray.hit position , [1,0,0]
if mouse.positive:
ray.hitObject.applyForce(force, local)
main()


This code does way too much:

• When running your game and you have custom Python code ALWAYS have a console window open, to see if there are errors!

• import only once. There is no need to import an already imported (sub)module.

• This code wants to enable the system mouse cursor. This has nothing to do with what you want to achieve. Remove that (it is incorrect syntax anyway).

• def main() serves no purpose. It disrupts the reading flow as it forces a reader to jump from top to bottom and back while reading your code [Imagine you read a novel that way]. The name "main" does not serve any information. Better leave the code outside of the function or give it a name that tells what the code is supposed to do. As it is the only function in your script ... the script name should do that already.

• Be careful with attribute names: sensor is not the same as sensors

• Be careful with syntax. Function names are followed by () rather than []

• Try to isolate debug code (drawing a line) from business code (applying a force). Otherwise it makes the code unnecessary complex. [If drawing a line is the business you forget to tell it above]

This leaves the code with:

import bge

controller = bge.logic.getCurrentController()

hit = controller.sensors["ray"]
mouse = controller.sensors["mouse"]

if hit.positive and mouse.positive:
strength = 1
force = hit.rayDirection * strength
hit.hitObject.applyForce(force)


Unfortunately you did not specify what force should be applied to. You can modify the line force = to apply whatever force you like. The above snippet is just an example. Without more information there is not much to provide.

• I wanted to assign this code to a bullet so that it has a force effect rather than bounce off. Can you please add a your force that i can tweak? Hope one day I'll be good in coding like you. :-) thank you. – innocent baraka Dec 20 '16 at 0:25
• 1) In that case this is more a projectile or arrow behavior. You get a small fast-travelling projectile on a ballistic path. 2) Small very fast projectiles are typically measured from the launcher (weapon), resulting in an immediate impact on the target (no travel time). This is what your code is good for. 3) slow travelling medium to large projectiles can be completely handled by the physics engine. You to measure the collision to remove the projectile on impact rather than to apply forces. Which one are you aiming for 1), 2) or 3) – Monster Dec 20 '16 at 5:37
• Sorry, i lost my aunt i couldnt reply early.id appreciate knowing each of them if possible or 2 if u don't have the time – innocent baraka Jan 9 '17 at 20:34
• Option 2) is what the solution describes. Measure the ray from launcher. – Monster Jan 10 '17 at 6:25