I have one problem I need help with.

I'm trying to measure a distance between the origin point and hit point using ray_cast. Is it possible or do I have to use another tool?

ray_cast loc[2] always return 1.0 or -1.0

cb = bpy.data.objects['Cube']
src = bpy.data.objects['src']
dst = bpy.data.objects['dst']

mw = cb.matrix_world
mwi = mw.inverted()

origin = mwi * src.matrix_world.translation
dest = mwi * dst.matrix_world.translation
direction = (dest - origin).normalized()

hit, loc, norm, face = cb.ray_cast(origin, direction)

enter image description here

Thank you for your answer.

  • $\begingroup$ Please add your code. Suspect the location is the location of hit. You want to subtract origin from it to get the distance $\endgroup$
    – batFINGER
    Apr 5, 2019 at 14:43

1 Answer 1


The location of hit loc is the local coordinate on the cube face that the ray hits.

Its local distance from origin

(origin - loc).length

its global distance from origin

(mw * loc - src.matrix_world.translation).length

With no rotation or scale applied all local coordinates on default cube are either 1 or -1. The top and bottom faces are the planes z = 1 and z = -1 respectively.

  • $\begingroup$ It works but I need to measure the length that ray "flown" before it hit the object. I'm planning to use it to find the highest and lowest point if terrain mesh with objects on it. $\endgroup$
    – Ján Hanes
    Apr 7, 2019 at 21:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .