[SOLVED] Simple use of pc.RaycastResult. Cannot get arbitrary enity-name output

Why cant I do this:

Raycast.prototype.doRayCast = function (screenPosition) {
    // Initialise the ray and work out the direction of the ray from the a screen position
    this.cameraEntity.camera.screenToWorld(screenPosition.x, screenPosition.y, this.cameraEntity.camera.farClip, this.ray.direction); 
    var result = this.aabbShape.intersectsRay(this.ray, this.hitPosition);        
    if (result) {
       // this.hitMarkerEntity.setPosition(this.hitPosition);
        console.log("result"+ result);
                var from = this.entity.camera.screenToWorld(screenPosition.x, screenPosition.y, this.entity.camera.nearClip);
                var to = this.entity.camera.screenToWorld(screenPosition.x, screenPosition.y, this.entity.camera.farClip);
    console.log("gregr: "+from + " :: "+to);
          //this.app.systems.rigidbody.raycastFirst(from, to, function (result2) {
        var point = new pc.Vec3(); var normal = new pc.Vec3();
        var result2 = this.RaycastResult(this.resEntity, point, normal);//this.aabbShape.intersectsRay(this.ray, this.hitPosition); 
                    console.log("Entity picked is: " + this.resEntity.entity.name);
         // });

By the way, the documentation here: https://developer.playcanvas.com/en/api/pc.RaycastResult.html

  • is poorly written/structured. No actual method/how-to-use code is displayed.

An instance of pc.Raycast result gets returned from this.app.systems.rigidbody.raycastFirst as show in the documentation here: https://developer.playcanvas.com/en/api/pc.RigidBodyComponentSystem.html#raycastFirst

The result contains the data shown in the documentation which includes the entity that was hit by the raycast: https://developer.playcanvas.com/en/api/pc.RaycastResult.html#entity

So you would use it (as seen in an example here: https://developer.playcanvas.com/en/tutorials/entity-picking-using-physics/)

Raycast.prototype.doRaycast = function (screenPosition) {
    // The pc.Vec3 to raycast from
    var from = this.entity.getPosition();
    // The pc.Vec3 to raycast to 
    var to = this.entity.camera.screenToWorld(screenPosition.x, screenPosition.y, this.entity.camera.farClip);

    // Raycast between the two points
    var result = this.app.systems.rigidbody.raycastFirst(from, to);
    // If there was a hit, store the entity
    if (result) {
        var hitEntity = result.entity;
        // Set the target position of the lerp script based on the name of the clicked box
        if (hitEntity.name == '1')
            this.entity.script.lerp.targetPosition = this.firstPosition;
        else if (hitEntity.name == '2')
            this.entity.script.lerp.targetPosition = this.secondPosition;

@YauStar is quite right, and will mark as solved. Still: I hope that base code documentation has better foundation within real-life examples on forward. Yes, the tutorial approach is already great, but as a medium-javascripter I still consider https://developer.playcanvas.com/en/api/pc.RaycastResult.html to be too theoretical/less practical.

Threads likes this do help me and others I guess, thanks …