How to play shockwave effect on spacebar press

i took the shockwave script from the shockwave tutorial,
i would like to know how to make it instead of playing when i click, i want it to play the effect when i press the spacebar, pretty self explanatory, just tell me what to change/add or just give me the script and this can be a quick and easy fix

https://playcanvas.com/editor/code/922637?tabs=8200308

var Clicker = pc.createScript('clicker');

// initialize code called once per entity
Clicker.prototype.initialize = function() {

};

// update code called every frame
Clicker.prototype.update = function(dt) {
    if (this.app.keyboard.wasPressed(pc.KEY_SPACE)) {
        var canvas = this.app.graphicsDevice.canvas;
        var x = e.x / canvas.width;
        var y = 1.0 - (e.y / canvas.height);
        this.entity.script.shockwave.center.set(x, y);
        this.entity.script.shockwave.play(); 
    }
};
2 Likes

doesn’t seem to work

I see. e is normally the mouse event position to determine where the shockwave will occur. What is the result if you disable the first four lines in the if statement?

You can also use the player position to set the center of the shockwave, but I don’t know off the top of my head how to do this.

1 Like

With the script below it is always in the middle of the screen I think, but I have not tested it.

var Clicker = pc.createScript('clicker');

// initialize code called once per entity
Clicker.prototype.initialize = function() {

};

// update code called every frame
Clicker.prototype.update = function(dt) {
    if (this.app.keyboard.wasPressed(pc.KEY_SPACE)) {
        var canvas = this.app.graphicsDevice.canvas;
        var x = canvas.width/2;
        var y = canvas.height/2;
        this.entity.script.shockwave.center.set(x, y);
        this.entity.script.shockwave.play(); 
    }
};

If you want the shockwave to start somewhere over an object, then use https://developer.playcanvas.com/api/pc.CameraComponent.html#worldToScreen to get the screen position of an object in the world and use that for the x and y

1 Like