Sudden scope loss on already defined entity

This doesn’t work while put on Camera entity in heirarchy - why?

var Raycast = pc.createScript('raycast');

Raycast.attributes.add('hovered',{
    type: 'boolean',
    default: false
});


Raycast.attributes.add('hitEntity',{
    type: 'entity'
});

Raycast.attributes.add('from',{
    type: 'Vec3'
});

Raycast.attributes.add('to',{
    type: 'Vec3'
});


Raycast.attributes.add('result',{
    type: 'entity',
    default: false
});

Raycast.attributes.add("offsetCurve", {type: "curve", title: "Offset Curve"});
Raycast.attributes.add("duration", {type: "number", default: 1, title: "Duration (secs)"});
 

// initialize code called once per entity
Raycast.prototype.initialize = function() {
    // Find the first entity in the hierarchy with the name 'Camera'
    this.cameraEntity = this.app.root.findByName('Camera');
    

};

Raycast.prototype.mouseMove = function (e) {
    this.doRaycast(e);
};



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

    // Raycast between the two points
     result = this.app.systems.rigidbody.raycastFirst(from, to);
    
    // If there was a hit, store the entity
    if (result) {
        hitEntity = result.entity;
        if (hitEntity.name == 'Disc_Sphere'){
             hitEntity.startScale = hitEntity.getLocalScale().clone();
            console.log(hitEntity.startScale.x );
              this.hovered = true; 

        }          

    }else{
        this.hovered = false;
    }
   
};

Raycast.prototype.update = function(dt) {
    // Loop the animation forever
    this.time += dt;
    if (this.time > this.duration) {
        this.time -= this.duration;
    }    
    
            // Calculate how far in time we are for the animation
        var percent = this.time / this.duration;

        // Get curve values using current time relative to duration (percent)
        var curveValue = this.offsetCurve.value(percent);


            if(this.hovered===true){
                hitEntity.startScale = hitEntity.getLocalScale().clone();
                console.log('2nd: '+hitEntity.startScale.x );
                hitEntity.scale.copy( hitEntity.startScale);
                hitEntity.scale.x += curveValue;
                hitEntity.scale.y += curveValue;
                hitEntity.scale.z += curveValue;

                hitEntity.entity.setLocalScale(hitEntity.scale);

                }else{       
       //  hitEntity.scale.x = this.startScale.x;  hitEntity.scale.y = this.startScale.y;  hitEntity.scale.z = this.startScale.z;   
         }
};
  • I get errors on these lines:
               hitEntity.scale.copy( hitEntity.startScale);
                hitEntity.scale.x += curveValue;
                hitEntity.scale.y += curveValue;
                hitEntity.scale.z += curveValue;

‘hitEntity’ should be defined, and works on the lines prior to this compiler point.

Hi Thomas,

Try replacing hitEntity with this.hitEntity

Thanks Mal_Duffin. Unfortunately I have already been down that road (several tries and workarounds :-/ )

It needs to be this.hitEntity everywhere, including in doRaycast and update.

If that doesn’t work, post a sample scene and I can have a look at it.

ok, here it is
(get a sent of bugs, though)

https://playcanvas.com/editor/scene/688383

RollOver the “discovery” button

Hi Thomas,

I forked the project and made a few changes to the raycast script, the code compiles and works now on the bottom two discs. If you copy the code in that script back into your own project, it should run the same, and will then allow you to continue with the scene.

The issue you were seeing was that the this.hitEntity.localScale property was not defined ( so it couldn’t find the .copy function )

https://playcanvas.com/editor/scene/688750

Hope that helps a bit!

Sure it helps - and thanks
For the future I will change my own debugging approach, as I wouldn’t have been able to find this ‘the normal way’/by my own approach
Clearly this type of debugging issue demands that one isolate two layers; first on/off on the ‘this.’-problem alone, for then to find the ‘.copy’-sub-issue below (F12 didn’t give me the .copy-error dispite all my tries up till now/your solution)