0
$\begingroup$

Here is my code:

import bge
from bge import logic

def property(controller):    
    own = controller.owner

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

target = scene.objects['Target']
ray = controllor.sensors['Ray']

if (ray.hitObject and 'Collide' in ray.hitObject and not 'Body' in ray.hitObject):
    target.worldPosition = ray.hitPosition
elif (ray.hitObject and 'Collide' in ray.hitObject and 'Body' in ray.hitObject):
    target.worldPosition = ray.hitPosition
    own['Body_Track'] = True

I have not finished with my code, but that is what I made so far. I am trying to make different props true and false depending on what you are aiming at: an object, and enemy (, rest WIP). This is to determine what color bullethole/blood should be added. For some reason, the last line of the code won't work. I always have issues with making multiple outcomes after an if. How can I fix this? Any help is appreciated. Thanks!

$\endgroup$
1
$\begingroup$

I suggest to redesign your code:

import bge

scene = bge.logic.getCurrentScene()
controller = bge.logic.getCurrentController()
owner = controller.owner

raySensor = controller.sensors['Ray']
if raySensor.positive:
    if 'Body' in raySensor.hitObject:
        target = scene.objects['Target']
        target.worldPosition = raySensor.hitPosition

    owner['Body_Track'] = True

Recommendations:

  • Filter "Collide" at the ray sensors configuration. All hitObjects have a property called "Collide". This way you get much less hitObjects and you do not need to filter by yourself.
  • No need to import bge and bge.logic. One of them should be enough.
  • Writing the exact same code several times is a sign of the need to redesign the code.
  • Nothing in this code ever resets the property "Body_Track". That might lead to confusion.
  • No call to "property()" - this makes this function obsolete.
$\endgroup$
  • $\begingroup$ Thanks for the code, but I don't understand why it isn't working. The code seems good to me. I have an object, which is the child of the target, and the object stays in its position. It isn't following the rays. $\endgroup$ – blackhole Jul 10 '15 at 23:35
  • $\begingroup$ if the properties do not change, the above conditions are not met. Either there is no hit ("Body_Track" would not change), or the hit object has no property "Body" (case sensitive). You can find that out by adding print statements after the condition checks. e.g. print("hitObject", raySensor.hitObject) $\endgroup$ – Monster Jul 14 '15 at 10:37
  • $\begingroup$ I've checked this a million times and it won't work. I tried what you said, but it isn't working and I'm puzzled why. $\endgroup$ – blackhole Jul 15 '15 at 1:19
  • $\begingroup$ can you post a demo file? $\endgroup$ – Monster Jul 15 '15 at 8:09
  • $\begingroup$ Well I don't give out my blend files but I just have an empty with a ray and script. $\endgroup$ – blackhole Jul 16 '15 at 13:14
0
$\begingroup$

You have own = controller.owner in a function that is never called.

In the case you have shown putting own into a function makes it local to the function, so calling the function still won't set the variable you use later.

The easiest change would be to set own after you set controller

$\endgroup$
  • $\begingroup$ Thanks for the help, but it still isn't working. $\endgroup$ – blackhole Jul 9 '15 at 7:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.