Using NodeCanvas to create simple bullet-spawning logic
It doesn't look like much but NodeCanvas takes a little getting used to since my Statemachines were all created as assets rather than bound to the object in the scene. This means passing references to other scene objects via variables assigned in the scene's blackboard. It's basically like programming except that you need to use several more windows but once set-up, the references are there for all other scripts (like with a Singleton).
This video shows what I set up in order to have 3 Statemachines running in sequence:
- Blackboard in the scene with references to the Shooter object, the container object for projectiles, and the projectile prefab to instantiate.
- PlayerInput FSM reacts to the "Fire" action button (so it works with any input device) and sends a "FireSignal" (an asset) to the "Shooter" object in the scene.
- Shooter FSM waits for FireSignal and then instantiates the projectile prefab.
- Projectile FSM waits for a collision three times in a row and at the end destroys itself. This can also be done by adding a CollisionCount variable to the FSM's blackboard, increasing it by 1 on every collision and adding another transition and state that activates when the CollisionCount exceeds a certain value.