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.
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.
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