4
$\begingroup$

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

enter image description here

enter image description here                                    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. enter image description here

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?

$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – christai Mar 9 '15 at 6:34
4
$\begingroup$

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

$\endgroup$
  • $\begingroup$ Is true level triggering set on either always sensor? $\endgroup$ – X-27 the fluffy unicorn Mar 8 '15 at 0:21
  • $\begingroup$ yes Always(true)--------Python $\endgroup$ – BluePrintRandom Mar 8 '15 at 0:27
  • $\begingroup$ Let me know if you need more help. $\endgroup$ – BluePrintRandom Mar 8 '15 at 0:27
  • $\begingroup$ Do you use module or script in the python controller? $\endgroup$ – X-27 the fluffy unicorn Mar 8 '15 at 1:33
  • $\begingroup$ 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! :-) $\endgroup$ – X-27 the fluffy unicorn Mar 8 '15 at 13:40

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.