Get ray length

Hi All,

Does anyone know of a way to get the distance to the hit position of a ray from the ray sensor?

The only way I can think of at the moment is to set the position of an empty to the hit position then use the getDistanceTo method. This seems rather complicated though.

The reason I ask is that I have a variety of things I want to activate with a ray sensor but I don’t want to have to have a different ray sensor for every different distance.

Thanks

getDistanceTo takes vectors as well as game objects:

ob = ...
ray = ob.sensors['ray']
dis = ob.getDistanceTo(ray.hitPosition)

Alternatively (not recommend unless you can’t use getDistanceTo) you can use Pythagoras:

from math import sqrt

p1 = ob.position
p2 = ray.hitPosition
x = p1[0]-p2[0]
y = p1[1]-p2[1]
z = p1[2]-p2[2]
dis = sqrt(x**2 + y**2 + z**2)

I did try the vector method. I just get an error saying - expect a KK_GameObject or a string?

Nevermind - I’m so dense - it was just a typo :(. Thanks andrew101.