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.