have you tried putting the debug statements in eg print rayhitdis, and seeing if what you were expecting is being returned.
so it would look something like this(may work).
function raycast()
rayhit = phy ray cast all shapes (camera position x(),camera position y(),camera position z(),0,-1,0)
if phy get ray cast hit()> 0 and phy get ray cast object() <= currentobj+1 and mouseclick()=1
print "entered if statement"
rayhitid = phy get ray cast object()
rayhitdis = phy get ray cast distance()
rayhitx# = phy get ray cast hit point x()
rayhity# = phy get ray cast hit point y()
rayhitz# = phy get ray cast hit point z()
position object rayhitid,rayhitx#-mousex(),rayhity#-mousey(),rayhitz#-mousez()
print rayhitid
print rayhitdis
print rayhitx#
print rayhity#
print rayhitz#
sync
wait key
endif
endfunction
now if it works ok but return the wrong number to what you thought would be returned this should show that, plus it will show if the if statement was true.
Dark Physics makes any hot drink go cold.