- Go to
Window > Package Manager. - Search for
Input System. - Click
Install. - Right-click in your Project window and select
Create > Input Actions. Give it a descriptive name likePlayerInputActions. - Double-click the newly created asset to open the Input Actions editor.
- Action Maps: Think of these as different contexts for your input. For example, you might have one for
Gameplay, another forUI, and so on. You can switch between them as needed. - Actions: These are the specific actions your player can perform (e.g., Move, Jump, Fire).
- Bindings: These link the actions to specific input devices and controls (e.g., W key, A key, left stick on a gamepad).
Hey guys! Ready to dive into creating some slick movement in Unity using the New Input System? You've come to the right place. This guide will walk you through everything you need to know, from setting up the input actions to writing the code that makes your character zoom around the game world. Let's get started!
Setting Up the New Input System
First things first, let’s get the New Input System installed. If you're starting a fresh project in Unity 2019.3 or later, it might already be included. If not, or if you’re working on an older project, here’s how to add it:
Unity will likely prompt you to restart the editor. Go ahead and do that – it’s necessary for the changes to take effect. Now that we have the package installed, let's configure some input actions. Input Actions are the heart of the New Input System. They allow you to define actions (like move, jump, fire) and map them to different input devices (keyboard, gamepad, mouse, etc.). This abstraction makes your code much cleaner and easier to manage.
To create Input Actions:
Inside the editor, you’ll see a few sections:
Let's create a Move action. Click the + button in the Actions section and name it Move. Change the Action Type to Value and Control Type to Vector2. This is perfect for handling movement in two dimensions (horizontal and vertical). Now, let's add some bindings to this action. Click the + icon next to Move and select Add Up/Down/Left/Right Composite. This creates a set of bindings that you can customize. By default, it should include bindings for the W, A, S, and D keys. You can also add bindings for the arrow keys or a gamepad's left stick. Feel free to customize these to your liking. The beauty of the New Input System is its flexibility.
With the Move action set up, you might want to add other actions like Jump and Fire. For Jump, you can create a new action with the Action Type set to Button and bind it to the spacebar. For Fire, you can do the same and bind it to the left mouse button. Remember to save your Input Actions asset by clicking the Save Asset button at the top of the editor. Now that we have our Input Actions configured, let's jump into writing some code to make our character move.
Writing the Movement Script
Now for the fun part – scripting! We’ll create a simple script that reads the input from our Input Actions and applies it to a character controller. First, create a new C# script called PlayerMovement and attach it to your player game object. Make sure your player has a CharacterController component attached as well; it's essential for collision detection and movement.
Here’s a basic script to get you started:
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour
{
private CharacterController controller;
private PlayerInputActions playerInput;
private Vector2 moveInput;
public float moveSpeed = 5f;
void Awake()
{
controller = GetComponent<CharacterController>();
playerInput = new PlayerInputActions();
playerInput.Gameplay.Enable();
playerInput.Gameplay.Move.performed += context => moveInput = context.ReadValue<Vector2>();
playerInput.Gameplay.Move.canceled += context => moveInput = Vector2.zero;
}
void Update()
{
Vector3 moveDirection = new Vector3(moveInput.x, 0, moveInput.y);
moveDirection = transform.TransformDirection(moveDirection);
controller.Move(moveDirection * moveSpeed * Time.deltaTime);
}
private void OnEnable()
{
if (playerInput != null)
{
playerInput.Gameplay.Enable();
}
}
private void OnDisable()
{
if (playerInput != null)
{
playerInput.Gameplay.Disable();
}
}
}
Let's break down this script:
CharacterController controller;: This variable holds a reference to theCharacterControllercomponent attached to the player.PlayerInputActions playerInput;: This is an instance of the Input Actions class that Unity automatically generates from your Input Actions asset.Vector2 moveInput;: This stores the input from theMoveaction as a 2D vector.public float moveSpeed = 5f;: This is the speed at which the player moves. You can adjust this value in the Inspector.Awake(): This method is called when the script instance is being loaded. Here, we get a reference to theCharacterController, create an instance ofPlayerInputActions, enable theGameplayaction map, and subscribe to theperformedandcanceledevents of theMoveaction.Update(): This method is called every frame. Here, we create a movement direction vector based on themoveInput, transform it to world space, and then use theCharacterControllerto move the player.OnEnable()andOnDisable(): These methods are called when the script is enabled and disabled, respectively. Here, we enable and disable theGameplayaction map to ensure that the input is only active when the player is in the gameplay context.
To make this work, you need to ensure that the PlayerInputActions class is properly generated. Go back to your Input Actions asset in the Project window. In the Inspector, you should see a checkbox labeled Generate C# Class. Make sure this is checked. If it isn't, check it and Unity will automatically generate a C# class with the same name as your asset (e.g., PlayerInputActions).
Now, attach the PlayerMovement script to your player game object, and make sure the player has a CharacterController component attached. Adjust the moveSpeed variable in the Inspector to your liking. Hit play, and you should be able to move your character around using the W, A, S, and D keys or the arrow keys. If you've set up gamepad bindings, you should be able to use the left stick as well.
Enhancing the Movement
So, you’ve got basic movement working, but let’s take it a step further and add some enhancements. Here are a few ideas:
Adding Jumping
To add jumping, you'll need to modify the PlayerMovement script to handle the Jump action. Here’s how you can do it:
-
Add a
Jumpaction to your Input Actions asset, set its Action Type toButton, and bind it to the spacebar. -
In the
PlayerMovementscript, add a variable to store the jump force:| Read Also : Wholesale Outdoor Sports Gearpublic float jumpForce = 5f; private bool isGrounded; -
In the
Awake()method, subscribe to theperformedevent of theJumpaction:playerInput.Gameplay.Jump.performed += context => Jump(); -
Add a
Jump()method to the script:void Jump() { if (isGrounded) { // Apply an upward force to the character controller. } } -
Modify the
Updatemethod to check if the player is grounded and apply gravity:void Update() { isGrounded = controller.isGrounded; if (isGrounded) { // Reset vertical velocity when grounded } // Apply gravity Vector3 moveDirection = new Vector3(moveInput.x, 0, moveInput.y); moveDirection = transform.TransformDirection(moveDirection); controller.Move(moveDirection * moveSpeed * Time.deltaTime); }
Smoothing Movement
Jerky movement can be a real immersion killer. To smooth things out, you can use the Mathf.Lerp function to interpolate between the current and target movement vectors. Here’s how:
-
Add a variable to store the current movement velocity:
private Vector3 currentVelocity; public float smoothTime = 0.1f; -
Modify the
Update()method to useMathf.SmoothDamp:void Update() { Vector3 targetVelocity = new Vector3(moveInput.x * moveSpeed, 0, moveInput.y * moveSpeed); currentVelocity = Vector3.SmoothDamp(currentVelocity, targetVelocity, ref currentVelocity, smoothTime); controller.Move(currentVelocity * Time.deltaTime); }
Adding Camera Control
A good camera is essential for a great player experience. You can add camera control by using the Mouse X and Mouse Y input axes to rotate the camera around the player. Here’s a basic example:
-
Create a new script called
CameraControllerand attach it to your main camera. -
Add the following code to the script:
using UnityEngine; public class CameraController : MonoBehaviour { public float sensitivity = 2f; public Transform playerBody; float xRotation = 0f; void Start() { Cursor.lockState = CursorLockMode.Locked; } void Update() { float mouseX = Input.GetAxis("Mouse X") * sensitivity; float mouseY = Input.GetAxis("Mouse Y") * sensitivity; xRotation -= mouseY; xRotation = Mathf.Clamp(xRotation, -90f, 90f); transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f); playerBody.Rotate(Vector3.up * mouseX); } }
Best Practices and Troubleshooting
Always Test on Multiple Devices
The New Input System is designed to support multiple input devices, so make sure you test your game on different devices to ensure that the input feels good on all of them.
Use the Input Debugger
Unity provides an Input Debugger that allows you to monitor the state of your input devices in real-time. This can be invaluable for troubleshooting input issues.
Keep Your Input Actions Organized
As your game grows, your Input Actions asset can become quite large. To keep things organized, consider using multiple Action Maps to group related actions together.
Avoid Hardcoding Input
One of the biggest advantages of the New Input System is that it allows you to avoid hardcoding input. Always use Input Actions to define your input, and never directly reference input devices in your code.
Check for Conflicting Inputs
Sometimes, two different actions can be bound to the same input. This can lead to unexpected behavior. Use the Input Debugger to check for conflicting inputs and resolve them.
Conclusion
So there you have it! You’ve learned how to set up the New Input System, create Input Actions, write a movement script, and enhance your movement with jumping, smoothing, and camera control. The New Input System is a powerful tool that can greatly improve the quality and maintainability of your game. Keep experimenting, and don’t be afraid to dive deeper into the documentation to discover all the features it has to offer. Happy coding, and I’ll see you in the next one! Peace out!
Lastest News
-
-
Related News
Wholesale Outdoor Sports Gear
Alex Braham - Nov 12, 2025 29 Views -
Related News
Santos Vs Flamengo: Thrilling Match Analysis
Alex Braham - Nov 9, 2025 44 Views -
Related News
Psubaru Restaurant: Semtse Barker's Culinary Gem
Alex Braham - Nov 13, 2025 48 Views -
Related News
Vanguard Group Net Worth: A Detailed Overview
Alex Braham - Nov 12, 2025 45 Views -
Related News
Ioscistepsc Tech In Clackamas: A Detailed Overview
Alex Braham - Nov 12, 2025 50 Views