I have implemented a laser function that fires when the user presses the Space Bar. I’m using a simple if statement with app.keyboard.wasPressed(pc.KEY_SPACE) being the condition. After a random amount of time, the laser stops responding to the user pressing the Space Bar. At first I thought the issue was with the function, but after console logging when the Space Bar is pressed, it shows that the game no longer recognizes when the Space Bar is pressed.
I have tested this on multiple devices and it is persistent on all non-touch devices.
The project can be found here and this is the only script using the app.keyboard.wasPressed(pc.KEY_SPACE) method.
The laserTest script is being destroyed for some reason. After some console.log's I have narrowed the destroying down to moving forward. So whenever the player presses “W”, the script will be destroyed sometime after.
I have tested shooting around just by rotating(only pressed “A” & “D” and not “W”) and it did not destroy the laserTest script. So it has to be something with moving forward. Currently I’m not doing much to move the player forward(see below). var p = this.entity.getPosition(); this.movement.copy(this.entity.forward).scale(this.speed * dt); this.entity.setPosition(p.x, p.y, this.z); this.entity.translate(this.movement);
The whole laserTestGeometry entity is being deleted. Which is very strange because that entity is a child of the Player entity, which obviously isn’t being deleted because the player doesn’t disappear or anything. The laserTest entity is not being deleted although. The hierarchy goes Player --> laserTest --> laserTestGeometry. The only entity that is being destroyed is the laserTestGeometry entity.
You are destroying the Entity that has the laserTest script attached and laserTest does it itself inside the raycastFirst callback. Basically laserTest performs a raycast, the raycast hits the Entity with the laserTest script and you call result.entity.destroy which destroys the Entity.
I found all that out by adding a ‘destroy’ method to your laserTest script while the game was running and put a breakpoint inside the destroy method to see where the destruction came from.
You probably don’t want the raycasting to hit the laserTest entity itself, so you could try adding some initial offset to the origin of the raycast so it always starts outside the Entity.