rayCastTo doesn't hit all over object

Hello, I’m using UPBGE 0.3

I’m learning to use “rayCastTo” function to do my own character’s jumping.

I use rayCastTo to detect if a character is on plane.
at start, I only press jump rayCastTo can detect and return Plane.
after I move around the plane, rayCastTo always return None.

I wonder why rayCastTo doesn’t hit all over the plane.

Here is my blend file
medievest_prototype_7.blend (2.6 MB)

you code works, self.isOnGround() is only checked in you jump function.
try place a

print("rayhit ",self.isOnGround() )

in you update function and you can see it works.

I try comparing this method to Ray sensor.

image

I don’t know why results of both are different. (as shown below)
although both rays are point to the same position.
Ray sensor found the wall as I expect, but rayCastTo didn’t.

Could you please explain me how to use rayCastTo properly?
Do you know how to display line ray’s line to debug it?

import bge
cont = bge.logic.getCurrentController()
if cont.sensors['Ray'].positive:
    end = ray.hitPosition 
    bge.render.drawLine(own.worldPosition, end, (0,0,1))
else:
    Distance = 2
    end = own.worldPosition - (own.worldOrientation.col[2]*distance )
    bge.render.drawLine(own.worldPosition, end, (1,0,0))

There’s the trap I also use to fall in.
While the logic brick ray goes to the local y-axis rayCast and rayCastTo go to worldcoordinate positions.
To get it to local Orientation of the object you can use:

from mathutils import Vector
RayCastTo_Vector =  own.worldOrientation @ Vector([0,1,0])

python rayCast would be more similar to the logic brick than rayCastTo but here is a demo for rayCastTo and the brick.

import bge
from mathutils import Vector

cont = bge.logic.getCurrentController()
own = cont.owner

Ray = cont.sensors['Ray']
if Ray.positive:
    end = Ray.hitPosition 
    bge.render.drawLine(own.worldPosition, end, (1,0,0)) #red 
else:
    end = own.worldPosition + Vector(Ray.rayDirection) * Ray.range
    bge.render.drawLine(own.worldPosition, end, (1,0.5,0.5)) # light red

RayCastTo_Vector =  own.worldOrientation @ Vector([0,1,0])
RayCastTo_range = Ray.range
RayCastTo_prop = Ray.propName
RayCastTo = own.rayCastTo(RayCastTo_Vector,RayCastTo_range, RayCastTo_prop )
if RayCastTo!=None:
    end = RayCastTo.worldPosition
    bge.render.drawLine(own.worldPosition, end, (0,1,0)) #green
else:
    end = own.worldPosition + RayCastTo_Vector * RayCastTo_range
    offset = Vector([0,0,0.1])
    bge.render.drawLine(own.worldPosition+offset, end+offset, (0.5,1,0.5))  #light green

UPBGE_03_RayCastTo.blend (828.8 KB)