I’m currently working on abstracting out input gathering into its own class that then calls on the other classes methods where input is needed. Is this something that is wise to do?
using System.Collections;
using UnityEngine;
public class InputCapture : MonoBehaviour
{
private CameraRotate camRotate;
private PlayerMovement playerMovement;
private WeaponBase weapon;
private WeaponListController weaponList;
private void Start()
{
this.camRotate = Camera.main.GetComponent<CameraRotate>();
this.playerMovement = GetComponent<PlayerMovement>();
this.weapon = GetComponentInChildren<WeaponBase>();
this.weaponList = GetComponentInChildren<WeaponListController>();
}
private void FixedUpdate()
{
#region Movement Input
//******************************************************************/
// Get input from the player and move FOR/BACK/LEFT/RIGHT depending
// on the keys pressed.
//******************************************************************/
float horzDir = Input.GetAxisRaw("Horizontal");
float forBackDir = Input.GetAxisRaw("Vertical");
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
this.playerMovement.Move(horzDir, forBackDir);
#endregion
}
private void Update()
{
#region Camera Rotation
if (Input.GetKeyDown(KeyCode.E))
this.camRotate.HandleInput(KeyCode.E);
if (Input.GetKeyDown(KeyCode.Q))
this.camRotate.HandleInput(KeyCode.Q);
#endregion
#region Shoot Weapon
if (Input.GetButton("Fire1"))
{
weapon.Shoot();
}
if (Input.GetButtonUp("Fire1"))
{
weapon.WeaponEffectOff();
}
#endregion
if (Input.GetKeyDown(KeyCode.Alpha1))
this.weaponList.SelectNextWeapon();
}
}
by all means have a separate function to handle all input (keyboard, controllers, touch, network, etc.) to set the state of each input device, but it's generally used by a player controller script and shouldn't directly call specific behaviours/actions - that's the player controller's job! i'm confused as to why you've made coroutines to do it, when you could easily handle in
– gjfUpdate()/FixedUpdate()/etc.It's generally smart to have a single class that handles all input. That makes it a lot easier to do things like redirecting input (when you are in a menu), preventing input (during cutscenes such) and so on. It should send it's input to some other controller class, and when you need to send input somewhere else, you just swap out what controller receives input from the input controller. Super easy. As Zionmoose says, there's no real need to have the input handling happen in a coroutine, though, at least not the input handling you're doing.
– BasteThe coroutine was practice and I realize that. I will be changing the code to update and fixed update.
– ZionmooseCode updated to not use coroutines.
– Zionmoose