3.6 KiB
3.6 KiB
Building System Input Actions Setup
Required Input Actions
Add these three actions to your InputSystem_Actions.inputactions file in the Player action map:
1. ToggleBuildMode (Button)
- Action Type: Button
- Control Type: Button
- Binding: Keyboard B
2. Rotate (Value)
- Action Type: Value
- Control Type: Axis
- Binding: Keyboard R (positive value)
- Alternative: You can use a 1D Axis composite with:
- Negative: Q
- Positive: E
3. Build (Button)
- Action Type: Button
- Control Type: Button
- Binding: Mouse Left Button
Step-by-Step Setup in Unity
-
Open Input Actions Asset
- Navigate to
Assets/InputSystem_Actions.inputactions - Double-click to open the Input Actions window
- Navigate to
-
Select Player Action Map
- In the left panel, click on "Player" action map
-
Add ToggleBuildMode Action
- Click the "+" button in the Actions column
- Name it:
ToggleBuildMode - Action Type: Button
- Add binding: Keyboard → B
-
Add Rotate Action
- Click the "+" button in the Actions column
- Name it:
Rotate - Action Type: Value
- Control Type: Axis
Option A (Simple - Single key):
- Add binding: Keyboard → R
Option B (Advanced - Q/E rotation):
- Right-click → Add 1D Axis Composite
- Negative: Keyboard → Q
- Positive: Keyboard → E
-
Add Build Action
- Click the "+" button in the Actions column
- Name it:
Build - Action Type: Button
- Add binding: Mouse → Left Button
-
Save and Regenerate
- Click "Save Asset" button
- The
InputSystem_Actions.csscript will auto-regenerate
How It Works in Code
The BuildingPlacement script subscribes to these actions:
// Setup in OnNetworkSpawn
_inputActions.Player.ToggleBuildMode.performed += OnToggleBuildMode;
_inputActions.Player.Rotate.performed += OnRotate;
_inputActions.Player.Build.performed += OnBuild;
// Cleanup in OnNetworkDespawn
_inputActions.Player.ToggleBuildMode.performed -= OnToggleBuildMode;
_inputActions.Player.Rotate.performed -= OnRotate;
_inputActions.Player.Build.performed -= OnBuild;
ToggleBuildMode
- Triggers when B is pressed
- Toggles build mode on/off
- Shows/hides building preview
Rotate
- Triggers when R is pressed (or Q/E if using composite)
- Reads value: positive = rotate right, negative = rotate left
- Rotates building preview by 90°
Build
- Triggers when left mouse button is pressed
- Only works when build mode is active
- Places the building at the preview position
Customizing Controls
To change the key bindings:
- Open
InputSystem_Actions.inputactions - Click on the binding you want to change
- Click "Path" dropdown
- Select new key/button
- Save Asset
No code changes needed - the script uses the action names, not specific keys!
Testing
After setup:
- Enter Play mode
- Press B → Should see "Entered Build Mode" in console
- Press R → Building preview should rotate
- Click Left Mouse Button → Should place building (if valid position)
- Press B again → Should exit build mode
Troubleshooting
Error: "ToggleBuildMode does not exist"
- Make sure you named the action exactly
ToggleBuildMode(case-sensitive) - Save the Input Actions asset to regenerate the code
Rotation doesn't work:
- Check the action name is exactly
Rotate - If using composite, make sure it's a 1D Axis composite
- Verify the action type is "Value" not "Button"
Build action triggers during movement:
- Make sure Build action is mapped to Mouse Left Button, not Attack
- Check that Build mode is active (press B first)