[SOLVED] Problems with raycasting

https://playcanvas.com/editor/scene/903136
Hi. I am trying to make the user able to pick up an object using RayCasting. However, it seems to always pick the floor when I click. It will disappear.

Also, I am new. Please do not judge me if I am too vague or anything.

I don’t know which script you are using for that, but i think you have to change a line in the pizza script.

As i can see goto is an entity and no position. So you have to change this:

result.entity.setPosition(goto);

to this:

result.entity.setPosition(goto.getPosition());

Thanks! I will try this as soon as my playcanvas stops glitching out. But, thank you again!

I think there is also another problem in your script.
If I start playing your game my laptop is crashing.

1 Like

I’m experiencing the same problem. It may be PlayCanvas, or the WiFi. I’m not sure.

No, because it is only your game.
Check your script for possible causes.
The problem starts when I click the mouse.

I think this is the problem:

PickerRaycast.prototype.onSelect = function (e) {
    var from = this.entity.camera.screenToWorld(e.x, e.y, this.entity.camera.nearClip);
    var to = this.entity.camera.screenToWorld(e.x, e.y, this.entity.camera.farClip);
    var goto = this.app.root.findByName("Text");
    this.app.systems.rigidbody.raycastFirst(from, to, function (result) {
        var pickedEntity = result.entity;
        do {
            result.entity.setPosition(goto.getPosition());
        } while (result.entity.name == "pizza");
    });
};

Try replacing it for this:

PickerRaycast.prototype.onSelect = function (e) {
    var from = this.entity.camera.screenToWorld(e.x, e.y, this.entity.camera.nearClip);
    var to = this.entity.camera.screenToWorld(e.x, e.y, this.entity.camera.farClip);
    var goto = this.app.root.findByName("Text");
    var result  = this.app.systems.rigidbody.raycastFirst(from, to);

    if (result) {
        var pickedEntity = result.entity;
        if (pickedEntity.name === "pizza") {
            pickedEntity.setPosition(goto.getPosition());
        }
    }
};

I need it to constantly be set to the users position while it is being held. That is why I use a “do… while” loop.

Nevermind, works as I wanted it! Thanks!

2 Likes