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.
Hi @Jacob_Jack,
I can’t run your game, I’m getting a number of errors.
For a projectile a general direction would be:
- Get the forward direction of an entity:
const dir = spaceship.forward;
- Calculate a force towards that direction:
const force = new pc.Vec3().copy(dir).mulScalar(100);
- 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);
- Now we fire the projectile by applying the force as an impulse:
projectile.rigidbody.applyImpulse(force);