How to properly raycast?

So I’m trying to see if my character can detect the edge of a layer & / or edge with a raycast so I can stop the character from twitching when getting ready to fall off. how to do this?

Been looking into raycasting lately as well. I’ll probably look at https://phaser.io/examples/v3/view/geom/intersects/point-to-line-segment and see what I can use.

Happy Thanksgiving.

@DaSoup : Happy Thanksgiving to you & yours as well! :slight_smile: This is a VERY interesting example I hadn’t come across, so thank you! :slight_smile: Please let me know your results on this post! :slight_smile:

@Thundros Found a better example: https://phaser.io/examples/v3/view/geom/intersects/line-to-rectangle

Not sure this functions as an actual ray. Like it would hit the closest object first instead of just the first object in the group, so this might need more work.

 this.group.children.iterate(function (child, index) {
    if (typeof child !== 'undefined') {
        // Raycasting Test
        recChild=new Phaser.Geom.Rectangle(child.body.left, child.body.top, child.body.right-child.body.left, child.body.bottom-child.body.top);
        this.boxChild = this.add.graphics({ lineStyle: { width: 2, color: 0xaa0000}});
        this.boxChild.strokeRectShape(recChild,2);
        if (Phaser.Geom.Intersects.LineToRectangle(this.lineRay, recChild)) {
            console.log("ray hit child: "+child.name);
            debugger;
        }
    }
}, this);
1 Like

For anyone still interested in raycasting, there’s a great plugin available now:

Thanks will check it out.