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?
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)


about scale

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