Hi! I decided to have a go with the new input system and make a simple top down 2D movement script with it. The script worked fine using the old system but now that I have converted it to the new system I have an issue. The input system needs a reference to Input mappings but when I have a public (or private, serialized) variable the field will not show up in the inspector. Here is the code I have:
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 4f;
public InputMaster controls;
private Rigidbody2D rb;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
controls.PlayerMap.Movement.performed += ctx => MovePlayer(ctx.ReadValue<Vector2>());
}
private void OnEnable()
{
controls.Enable();
}
private void OnDisable()
{
controls.Disable();
}
private void MovePlayer(Vector2 direction)
{
rb.velocity = direction.normalized * moveSpeed;
//TODO: Animate Sprite
}
}
I am getting no errors in the console and can’t see anything wrong with my code. Thanks for the help!
I had implemented the input system wrong because I was following an outdated tutorial. Whoops!
For anyone wondering, here is the working code:
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour, InputMaster.IPlayerMapActions
{
public float moveSpeed = 4f;
public InputMaster controls;
private Rigidbody2D rb;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
}
public void OnEnable()
{
if (controls == null)
{
controls = new InputMaster();
controls.PlayerMap.SetCallbacks(this);
}
controls.PlayerMap.Enable();
}
public void OnMovement(InputAction.CallbackContext context)
{
Vector2 direction = context.ReadValue<Vector2>();
rb.velocity = direction.normalized * moveSpeed;
//TODO: Animate Sprite
}
}
I spent way too long wondering what I was doing wrong before reading the comments on the brackeys tutorial.
video reference:
comment by user cbox reads:
“TIP For those doing the tutorial more
recently, you can no longer drag the
file into the inspector as they
changed the backend. You can just do
controls = new InputMaster(); in awake
to create a new instance of the
object.”
Essentially, IF your code looks like this:
`
using UnityEngine.InputSystem;
using UnityEngine;
public class ThirdPersonMovement : MonoBehaviour
{
public InputMaster controls;
void Awake()
{
controls.Player.Movement.performed += _ => Move();
controls.Player.Shoot.performed += ctx => Shoot();
}
void Move()
{
Debug.Log("Move");
}
void Shoot()
{
Debug.Log("Shot");
}
private void OnEnable()
{
controls.Enable();
}
private void OnDisable()
{
controls.Disable();
}
}
Then you just need to add **controls = new InputMaster();** in the awake function
using UnityEngine.InputSystem;
using UnityEngine;
public class ThirdPersonMovement : MonoBehaviour
{
public InputMaster controls;
void Awake()
{
// ADD NEW INSTANCE HERE-->
controls = new InputMaster();
controls.Player.Movement.performed += _ => Move();
controls.Player.Shoot.performed += ctx => Shoot();
}
void Move()
{
Debug.Log("Move");
}
void Shoot()
{
Debug.Log("Shot");
}
private void OnEnable()
{
controls.Enable();
}
private void OnDisable()
{
controls.Disable();
}
}
`