So I was trying to make a script so that if a click and grappling == false it turns it on and vice versa but i realize that since it reads from up to down it’s going to just go from on to back off.
script:
var GrapplingHook = pc.createScript('grapplingHook');
GrapplingHook.attributes.add("camera",{
type: 'entity',
})
GrapplingHook.attributes.add("player",{
type: 'entity',
})
GrapplingHook.attributes.add("point",{
type: 'entity',
})
// initialize code called once per entity
GrapplingHook.prototype.initialize = function() {
this.screenCenterX = this.app.graphicsDevice.width / 2;
this.screenCenterY = this.app.graphicsDevice.height / 2;
this.hidgePoint = null;
};
// update code called every frame
GrapplingHook.prototype.update = function(dt) {
this.start = this.camera.camera.screenToWorld(this.screenCenterX, this.screenCenterY, this.camera.camera.nearClip);
this.end = this.camera.camera.screenToWorld(this.screenCenterX, this.screenCenterY, this.camera.camera.farClip);
this.grappling = false;
this.doRaycast()
if (this.app.mouse.wasReleased(pc.MOUSEBUTTON_LEFT)){
console.log('Pressed')
if(this.result){
this.hidgePoint = this.result.point
this.ray = new pc.Ray(this.start, this.hidgePoint);
this.direction = new pc.Vec3().copy(this.hidgePoint).sub(this.start).normalize();
// now you can use the direction and scale it to become a force
this.power = new pc.Vec3().copy(this.direction).scale(10000);
if (this.grappling == false){
this.grappling = true
console.log("on")
}
}
if (this.grappling == true){
this.grappling = false
console.log('off')
}
}
// actually do the thing
if (this.grappling == true){
this.app.drawLine(this.start, this.hidgePoint, pc.Color.RED, true);
this.player.rigidbody.applyForce(this.power)
console.log("Grappling")
}
};
GrapplingHook.prototype.doRaycast = function() {
this.result = this.app.systems.rigidbody.raycastFirst(this.start, this.end);
}
// swap method called for script hot-reloading
// inherit your script state here
// GrapplingHook.prototype.swap = function(old) { };
// to learn more about script anatomy, please read:
// https://developer.playcanvas.com/en/user-manual/scripting/