So I decided I was going to make a top down game from scratch (mostly) using the new input system to learn it. All was going great, until I attempted to create an input for my character to run. I have diagnosed the problem, but I haven’t figured out how to solve it so I’m hoping someone could help me — this is a very weird specific problem and I haven’t found ANYTHING close to this in my search.
Set Up:
I have my player controller set up to use the new input system like this:
//Movement
private float moveSpeed;
public float runSpeed;
public float walkSpeed;
private PlayerInput playerInput;
private PlayerInputMap playerInputMap;
private InputAction move;
private InputAction run;
private Vector3 moveDirection = Vector3.zero;
//Events
public delegate void BeginMission();
public static event BeginMission StartMission;
private void Awake()
{
playerInput = GetComponent<PlayerInput>();
playerInputMap = new PlayerInputMap();
playerInputMap.Player.Enable();
move = playerInputMap.Player.Move;
run = playerInputMap.Player.Run;
}
private void Start()
{
moveSpeed = walkSpeed;
rb = GetComponent<Rigidbody>();
}
private void OnEnable()
{
run.Enable();
move.Enable();
}
private void OnDisable()
{
move.Disable();
run.Disable();
}
private void Update()
{
if (GameManager.Instance != null)
{
if (GameManager.Instance.CurrentState == GameManager.GameStates.Mission)
{
moveDirection = move.ReadValue<Vector3>();
}
else if (GameManager.Instance.CurrentState == GameManager.GameStates.Lose || GameManager.Instance.CurrentState == GameManager.GameStates.Win || GameManager.Instance.CurrentState == GameManager.GameStates.Start)
{
playerInput.SwitchCurrentActionMap("UI");
moveDirection = Vector3.zero;
}
else
{
moveDirection = Vector3.zero;
}
}
//else
//{
// moveDirection = move.ReadValue<Vector3>();
//}
}
public void OnRun(InputAction.CallbackContext context)
{
if (context.performed)
{
moveSpeed = runSpeed;
}
if (context.canceled)
{
moveSpeed = walkSpeed;
}
}
The key here is the OnRun function. I have my player input component on my character set to “Invoke Unity Events”, and the OnRun function should be called when I press the shift button — here is my Input Map
The expected result when I enter my scene is that the player character will “run”, change speed and animations when the shift button is held. The odd thing is, this works when I remove the Game Manager, but not when I have the Game Manager in my scene. The game manager is sort of important, you see, so I’m not sure how to fix this – especially since I have no errors or anything I can point to.
My game manager is very simple, it really just fires off events when the state is switched:
public static GameManager Instance;
public enum GameStates { Menu, Mission, Lose, Win, Start, Loading }
public GameStates _CurrentState;
public delegate void MissionComplete();
public static event MissionComplete missionCompleteEvent;
public delegate void MissionStart();
public static event MissionStart missionStartEvent;
public delegate void MissionFailed();
public static event MissionFailed missionFailedEvent;
public delegate void MissionActive();
public static event MissionActive missionActiveEvent;
private void Awake()
{
DontDestroyOnLoad(this);
}
private void Start()
{
Instance = this;
//CurrentState = GameStates.Menu;
}
public GameStates CurrentState { get { return _CurrentState; }
set
{
_CurrentState = value;
switch (CurrentState)
{
case GameStates.Menu:
break;
case GameStates.Mission:
if (missionActiveEvent != null) missionActiveEvent();
break;
case GameStates.Lose:
StopAllCoroutines();
if (missionFailedEvent!= null) missionFailedEvent();
break;
case GameStates.Win:
StopAllCoroutines();
if (missionCompleteEvent != null) missionCompleteEvent();
break;
case GameStates.Start:
if (missionStartEvent != null) missionStartEvent();
break;
}
}
}
}
This is driving me absolutely up the wall, because I can’t for the life of me figure out why the GameManager is the problem. Does anyone have any ideas? This would be greatly appreciated. I’ve been stuck on this for like 5 hours.
Thanks!