A problem about screenToWorld

I want to know whether the mouse points to an entity to change the pattern of the mouse. For example, when it points to an entity, the mouse changes from an arrow to a small hand, and I found this:
API: screenToWorld
API: raycastFirst
this is my code:

TouchEvent.prototype.onMouseMove = function(e){
    var camera = this.app.root.findByName('Camera').camera;
    var clickX = e.dx;
    var clickY = e.dy;
    var start = camera.screenToWorld(clickX, clickY, camera.nearClip);
    var end = camera.screenToWorld(clickX, clickY, camera.farClip);
    // Use the ray coordinates to perform a raycast
    this.app.systems.rigidbody.raycastFirst(start, end, function (result) { // line 44
        console.log("Entity " + result.entity.name + " was selected");
    });
}

but I got this error:

playcanvas-stable.dbg.js:43626 Uncaught TypeError: Cannot read property 'setValue' of undefined
    at RigidBodyComponentSystem.raycastFirst (playcanvas-stable.dbg.js:43626)
    at scriptType.TouchEvent.onMouseDown (TouchEvent.js?id=40894533&branchId=bd4b1e76-80cc-42f7-909e-4a2dc7758838:44)
    at Mouse.fire (playcanvas-stable.dbg.js:590)
    at Mouse._handleDown (playcanvas-stable.dbg.js:56219)
raycastFirst @ playcanvas-stable.dbg.js:43626
TouchEvent.onMouseDown @ TouchEvent.js?id=40894533&branchId=bd4b1e76-80cc-42f7-909e-4a2dc7758838:44
fire @ playcanvas-stable.dbg.js:590
_handleDown @ playcanvas-stable.dbg.js:56219

I don’t know why,help me please

Can you post a link to the project please?

Thank you for your time ,I found this just now:
changeMouseState
It almost solved my problem,but what I want more is to listen to any ordinary entity, not just a button,do you have any idea?

Without seeing the project, it’s difficult to help. Can you post a link to your project please?

this is the demo,see the mouse,it changed on button,it is ok,but other entity is not:
demo

I want to change the mouse when it point at any entity,but only button worked

The reason why raycast didn’t work is because the project doesn’t have the physics library added Ammo. Click on ‘Import Ammo’ here and you can use Physics based collision click as shown here: https://developer.playcanvas.com/en/tutorials/entity-picking/#collision-picking

You can use frame buffer picking as shown here as an alternative so that you don’t need to use the physics library https://developer.playcanvas.com/en/tutorials/entity-picking/#frame-buffer-picking

Since you are doing it every frame, I would go the physics route.

1 Like

You are so nice, as you say, collision-picking is the best choice for me,it totally solved my problem.
Thank you for your help!