Hi everyone!
I’m struggling with handling player input in my ECS project.
My architecture is organized into three main parts:
- Spawning the player using an event-driven pattern
- Camera following the player
- Updating the player’s position based on input🥲
What I did in the editor
- In a SubScene, create a 3D object of Cube for player and a Plane for the ground so I can see and confirm whether it can move (Unfortunately not, and that's the biggest problem I thought)
- Drag the Cube into Project to get a prefab
- Add a
PlayerAuthoring.csscript and aPlayer Inputcomponent (I saved the action) for key bindings andPlayerInputHandler.csscript, and the specific code is below. - Add a
CameraFollowAuthoring.csscript to the main camera. And the main scene is a pure container for SubScene in my opinion. - Run the game, found I cannot move the Cube. 🥲
And spawning the player and camera seems fine, at least I'm troubled by how to move the player.
The expected workflow should be as follows:
[PlayerInputHandler (MonoBehaviour)]
│ (OnMove sets MoveInput: Vector2)
▼
[InputSystem (SystemBase)]
│ (reads MoveInput)
▼
[InputData (ECS Singleton)]
│ (stores Move: float2)
▼
[PlayerMovementSystem (ISystem)]
│ (reads InputData + deltaTime)
▼
[PlayerMovementJob (IJobEntity)]
│ (executes per entity with MovementData + Transform)
▼
Updates Entity Transform.Position
And
1.1 (Scripts/Component/InputData.cs)
InputData— ECS component withMove : float2
1.2 (Scripts/Components/PlayerData.cs)
MovementData— ECS component withDirection : float2,Speed : float
2. (Scripts/MonoBehaviour/PlayerInputHandler.cs)
PlayerInputHandler— MonoBehaviour withMoveInput { get; private set; } : Vector2OnMove(InputValue value) => MoveInput = value.Get<Vector2>()
1/n