I’m trying to get my gun in my first game Fire! to fire bullets continually when I press and hold the x button. I also want to destroy the bullets once they reach a certain distance, but I don’t know enough about either PlayCanvas or coding to do that. What is an easy way (both for me and the computer) to set up something like that?
- Spawn an entity with a rigidbody and collider component. The rigidbody component should have damping set to 0.
- Apply a force to the entity in a given direction.
- Use the setTImeout() function to call
entity.destroy()after a certain amount of time has passed.
But how do I continuously spawn it that way?
You could spawn the bullets from another entity script’s update loop. If you’re confused about how to spawn entities, I suggest checking out this tutorial.
alright then. Thank you!
Um, I don’t know how to spawn the entity WITH rigidbody using code.
The simplest way would be to make a disabled bullet prefab in your scene and then clone it to spawn as needed:
var clonedEntity = this.app.root.findByName("BulletPrefab"); var newEntity = clonedEntity.clone(); this.app.root.addChild(newEntity); newEntity.enabled = true;