Broken Hero PlayerLaser Help

I need help trying to get the projectile / PlayerLaser to work with the ship. I’m trying to code it so when you press space, the laser shoots.

https://playcanvas.com/editor/scene/1630965

Hi @Jacob_Jack,

I can’t run your game, I’m getting a number of errors.

For a projectile a general direction would be:

  1. Get the forward direction of an entity:
const dir = spaceship.forward;
  1. Calculate a force towards that direction:
const force = new pc.Vec3().copy(dir).mulScalar(100);
  1. Position the projectile in front of your spaceship:
const shipPos = spaceship.getPosition().clone();

// we calculate a new world position 1 unit in front of the ship by adding the forward direction
const spawnPos = shipPos.add(dir);

// and we position the projectile there
projectile.rigidbody.teleport(spawnPos);
  1. Now we fire the projectile by applying the force as an impulse:
projectile.rigidbody.applyImpulse(force);