# Dynamically assign a value to game property

I am trying to make a weapon sight Dynamically scale depending on how far away the target is.

Desired effect

However right now it does not scale. the sight stays the same because there the distance property does not change.

(the action it plays is simply an animation of the sight scaling with frame 1 really big and frame 100 very small)

Logic setup for sight.

The sight is on an overlay scene. I already have a ray being cast from the camera and a script to move an empty to the hit position of the ray. The guns then tracks to that empty so it will always fire to the center of the screen. However the problem occurs when I try to get the distance between the world position of the camera, and the world position of the empty and assign it to the property Distance.

My question is this:

How can I get the distance between the camera's world position and the empty's world position and assign that value to a game property?

• even though this was just answered, would it be easier to use pythagorus theorem using the camera and empty's locations to get the distance from the camera to the empty? – christai Mar 9 '15 at 6:34

always ------------This python

it will set distance.

import bge
from mathutils import Vector
cont = bge.logic.getCurrentConroller()

own = cont.owner

offset = Vector([100,0,0])

offset = own.worldPosition+(own.worldOrienation*offset)

##offset now is a point 100 units out on the objects x axis

Ray = own.rayCast(offset , own.worldPosition, 0, '',0,0,0)
## cast a ray to the offset point, 0 units past, looking for any property, not xray and
##  Not returning polydata

if Ray[0]:
## if you hit something
own['Distance'] = own.getDistanceTo(Ray[1])
## if you dont hit something
else:
own['Distance'] = own.getDistanceTo(offset)


in object

always--------------and------------copy distance from raycaster

Distance is changed------python below

import bge
cont = bge.logic.getCurrentController()
own=cont.owner

own.localScale =[1-own['Distance']*.01,1-own['Distance']*.01,1-own['Distance']*.01]


Note,

Ray[1] = world Position Where Ray hit Ray[0] = who it hit

http://www.pasteall.org/blend/34899

• Is true level triggering set on either always sensor? – X-27 the fluffy unicorn Mar 8 '15 at 0:21
• yes Always(true)--------Python – BluePrintRandom Mar 8 '15 at 0:27
• Let me know if you need more help. – BluePrintRandom Mar 8 '15 at 0:27
• Do you use module or script in the python controller? – X-27 the fluffy unicorn Mar 8 '15 at 1:33
• If only I had thought about it logically from the start. Your script was firing the ray along the local X axis. The camera looks along the local -Z axis. Change the axis, problem solved. Thanks! :-) – X-27 the fluffy unicorn Mar 8 '15 at 13:40