//we must "return" after setting this prefab because if we were to continue into the Job
//we would run into errors because the variable was JUST set (ECS funny business)
//comment out return and see the error
shoot = 0;
var playerCount = m_PlayerQuery.CalculateEntityCountWithoutFiltering();
shoot = 1;
if (shoot == 1 && playerCount < 1)
For reasons not fully yet understood by man, adding InputSpawnSystem might cause errors through our multiple rounds of testing using this gitbook (although it seems like this has been fixed in 2020 LTS)
Classic working with preview packages!
If you get errors, go to "Assets" and choose "Reimport All"
This will fix the errors
Add InputSpawnSystem, but then 'Reimport All' assets due to errors in Editor
You will notice the similarities of InputSpawnSystem with AsteroidSpawnSystem, but also a few differences:
why aren't we using the EntityCommandBuffer in InputSpawnSystem?!
And what the heck is an EntityManager?!
We are not using the EntityCommandBuffer to demonstrate how to make structural changes during InputSpawnSystem's OnUpdate()
That's not to torture you, it's to teach you more ECS functionalities 💪
We will continue using the EntityCommandBuffer later on when we spawn bullets 😌
A World organizes entities into isolated groups. A world owns both an EntityManager and a set of Systems. Entities created in one world only have meaning in that world, but can be transfered to other worlds (with EntityManager.MoveEntitiesFrom). Systems can only access entities in the same world. You can create as many worlds as you like.
By default Unity creates a default World when your application starts up (or you enter Play Mode). Unity instantiates all systems (classes that extend ComponentSystemBase) and adds them to this default world. Unity also creates specialized worlds in the Editor. For example, it creates an Editor world for entities and systems that run only in the Editor, not in playmode and also creates conversion worlds for managing the conversion of GameObjects to entities. See WorldFlags for examples of different types of worlds that can be created.
A World owns a single EntityManager and a set of Systems
The EntityManager is what we use to interact with Entities if we are not using EntityCommandBuffer
The EntityManager provides an API to create, read, update, and destroy entities.
A World has one EntityManager, which manages all the entities for that World.
Many EntityManager operations result in structural changes that change the layout of entities in memory. Before it can perform such operations, the EntityManager must wait for all running Jobs to complete, an event called a sync point. A sync point both blocks the main thread and prevents the application from taking advantage of all available cores as the running Jobs wind down.
Although you cannot prevent sync points entirely, you should avoid them as much as possible. To this end, the ECS framework provides the EntityCommandBuffer, which allows you to queue structural changes so that they all occur at one time in the frame.
Now that we've created our InputSpawnSystem let's hit "play" then "space bar" to spawn our Player prefab
Spawning the Player prefab by hitting space bar
Woo, all set! Right?
Not quite, why isn't the camera updated to the Player prefab's Camera GameObject?
The Camera is a GameObject but is being instantiated through ECS... a little wonky
To fix this we need to add HYBRID_ENTITIES_CAMERA_CONVERSION to the Player Settings
Navigate to "File", choose "Build settings", then "Player Settings", then "Player" on the left, expand the drop down menu for "Other Settings", and scroll down to "Scripting Define Symbols". Add HYBRID_ENTITIES_CAMERA_CONVERSION
Click away from the Scripting Define Symbols field on the Project Settings for it to be picked up
This will cause a warning on the Build Settings window
Add HYBRID_ENTITIES_CAMERA_CONVERSION to Scripting Define Symbols
Wait until the warning on the Build Settings window goes away
Now hit "play" and press "space bar" again to see our new camera view
Through testing we have found that sometimes this change does not immediately "take"
In that case, go to "Assets" and choose "Reimport All"
Spawning a Player prefab now switches to the Camera GameObject attached to the Player
We can now spawn our player from user input
We created our player prefab
We added a PlayerTag and VelocityComponent to the prefab
We created InputSystem which takes in inputs and spawns our player
Moving a Player
We are going to create InputMovementSystem in Scripts and Prefabs to take in "WASD" and mouse input to change the rotation and velocity of our player
We could implement this functionality into the existing InputSpawnSystem (and name the system something else) but we are separating to make the code easier to understand
Create InputMovementSystem and paste the code snippet below into InputMovementSystem.cs:
public class InputMovementSystem : SystemBase
protected override void OnCreate()
//We will use playerForce from the GameSettingsComponent to adjust velocity