How to Raycast to the Mouse in Script Canvas

I’ve attached a basic scriptcanvas graph that will raycast from the camera through the mouse’s position onscreen. The graph has comments on the individual steps of the process. An input bindings file must be set to send the action “LeftClick” on mouse left-click. Additionaly, a UI canvas is needed to get the screenwidth and height in pixels. More information on the UI canvas is in the comments of the script canvas graph, but an example UI and input bindings are included in the zip file.

For the level setup the following entities and components are needed:

1 Entity with the components:
script canvas - load example graph
input - load example inputbindings file
ui (this can be on a separate entity) - load example UI canvas

At least 1 target entity with the components:
mesh collider (Legacy)
static physics (Legacy) or rigid physics (Legacy) or character physics (Legacy)

The raycast node used in the example scriptcanvas graph is the legacy version. If that node is changed to use the PhysX raycast node instead, PhysX components can be used on the target entities. (75.8 KB)

1 Like