My testing level, the enemy projectile should be shooting every second, but it doesn't shoot after a second and into a loop. Need Help Please :)

My enemy projectile isn’t shooting every second like it needs to be I would like if someone could help me out.

This is my Editor Link: PlayCanvas | HTML5 Game Engine

The focus is on enemy craft 2 projectile 2

Update:

My testing page now when I run it shows errors not sure how to fix it my player ship won’t move or shoot lasers anymore I don’t want there to be errors. I also have a power up laser that the script isn’t working.

Given directions I have:

Inside of this script, we can add the following to our initialize function to listen for when a collision occurs.

this.entity.collision.on('collisionstart', this.onCollisionStart, this);

Next, we can add a new function for this event and disable the entity.

Laserpowerup.prototype.onCollisionStart = function (result) {
        this.entity.enabled = 0;        
};

For our current inputHandler script, we need to make a few adjustments. First, we need to declare a new variable that we can check and update to different powerup levels. Let’s call this laserlevel. In your initialize function, add the following.

laserlevel = 1;

Now let’s add a new condition to our fire projectile statement to check if Spacebar is pressed AND the laserlevel is set to 1:

    if (this.app.keyboard.wasPressed(pc.KEY_SPACE) && laserlevel == 1) {
    var templateAsset = this.app.assets.find("PlayerLaser");
    var instance = templateAsset.resource.instantiate();      
    var position = this.entity.getPosition();        
    var entity = new pc.Entity();
    this.app.root.addChild(entity);

    entity.translate(position.x-2, position.y, position.z);
    entity.addChild(instance);   
    }

Great! Now we want to duplicate this script with a few adjustments for when laserlevel happens to be equal to 2 instead of 1. Notably, we’ll change the object spawned to a template that includes two separate lasers:

    if (this.app.keyboard.wasPressed(pc.KEY_SPACE) && laserlevel == 2) {
    var templateAsset = this.app.assets.find("PlayerLaser2");
    var instance = templateAsset.resource.instantiate();      
    var position = this.entity.getPosition();        
    var entity = new pc.Entity();
    this.app.root.addChild(entity);

    entity.translate(position.x-2, position.y, position.z);
    entity.addChild(instance);   
    }

Create a Second Laser Template

Now we can create a new PlayerLaser2 entity, then drag in two PlayerLaser templates into the Hierarchy. Adjust them so that they are next to each other. Save the PlayerLaser2 entity as a new template. Each of these projectiles will have its own script and components attached, so they will still act independently.

We can test this by manually changing the laserlevel variable in our script to 2 and then playing the game.

Add a Powerup Function

Now that it’s working, switch the laserlevel back to 1, and we can add a new function to adjust the level for us. This will work similarly to our score update function. First, still in the inputHandler script, add the following to our initialize function:

    var onPowerup = function(value) {
    laserlevel=value;
    };
    
    this.app.on('player:laserlevel', onPowerup, this);

Then, in your laserpowerup.js file, add the following to your onCollisionStart function to call on our onPowerup script:

this.app.fire('player:laserlevel', 2);

Now when we collide with an object with the laserpowerup tag, our laserlevel will be changed to 2.

:frowning: I’m stuck.

Any help would be greatly appreciated.

.

Hi @C11! :waving_hand:

  • To fix error Uncaught SyntaxError: Unexpected end of input add a } bracket on line 52 of your laserpowerup.js script.

  • To fix error Cannot read properties of undefined (reading 'on') add a collision component to the entity with the laserpowerup.js script.

Once these are fixed, I’ll check the other problem later today.

Good luck!

Thank you. I still have errors popping up and my testing isn’t working how it should.

Try to read the errors and fix them one by one.

I fixed the errors but i’m still having trouble with the enemy projectile shooting.

Had to start over so this is my Editor Link: PlayCanvas | HTML5 Game Engine