Hello, I am currently learning how to use the new unity input system as I am new yo unity.
I have been trying to update my shooting script with the new system and I am having trouble with setting my shoot key that I have setup up in the Input Actions
to a KeyCode
in unity. Is there anyone that could help me with this because I have searched everywhere but everything I have seen has not helped me out. Any help would be appreciated
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.InputSystem;
public enum FireMode {Single, Burst, Auto}
public class M4CARBINE : MonoBehaviour
{
[Header("KeyBinds")]
[SerializeField] private KeyCode _ShootKey = KeyCode.Mouse0;
[SerializeField] private KeyCode _ReloadKey = KeyCode.R;
[Space]
[Header("Enums")]
[SerializeField] private FireMode _fireMode = FireMode.Single;
[Space]
[Header("Refrences")]
[SerializeField] private Animator Anim;
[SerializeField] private Text Ammo;
[SerializeField] private GameObject BulletPrefab;
[SerializeField] private Transform BulletSpawnPos;
[SerializeField] private GameObject ShootPos;
[Space]
[Header("Gun Values")]
[SerializeField] private float FireRate;
[SerializeField] private float NextTimeToFire;
[SerializeField] private int CurrentAmmo = 40;
[SerializeField] private int MagCapacity = 140;
[SerializeField] private int MaxAmmo = 40;
[SerializeField] private float ReloadTime;
[SerializeField] private float BulletSpeed;
[SerializeField] private float BulletLife;
[SerializeField] private float TimeAfterReload;
[SerializeField] private float RayDist;
[Space]
[Header("Layers")]
[SerializeField] private LayerMask ShootAble;
[Space]
[Header("Strings")]
[SerializeField] private string ReloadAnimName;
[Space]
[Header("Bools")]
[SerializeField] private bool _ShootInput;
[SerializeField] private bool CanShoot;
[SerializeField] private bool ReloadPressed;
[SerializeField] private bool CanReload;
private InputMaster playerControls;
// private bool InputKey = playerControls.Player.Shoot.ReadValue();
private void Awake()
{
CanShoot = true;
_ShootInput = false;
ReloadPressed = false;
NextTimeToFire = 0f;
CurrentAmmo = MaxAmmo;
}
public void Update()
{
switch (_fireMode)
{
case FireMode.Auto:
_ShootInput = Input.GetKey(_ShootKey);
break;
case FireMode.Burst:
_ShootInput = Input.GetKeyDown(_ShootKey);
break;
case FireMode.Single:
_ShootInput = Input.GetKeyDown(_ShootKey);
break;
}
if(Input.GetKeyDown(_ReloadKey) && CurrentAmmo < MagCapacity && !ReloadPressed && CanReload)
{
StartCoroutine(Reload());
ReloadPressed = true;
}
if(CurrentAmmo <= 0)
{
CanShoot = false;
}
if(_ShootInput && Time.time >= NextTimeToFire && CanShoot)
{
Shoot();
NextTimeToFire = Time.time + 1f/FireRate;
}
Ammo.text = CurrentAmmo.ToString("0") + " / " + MagCapacity.ToString("0");
if (CurrentAmmo < MaxAmmo)
CanReload = true;
else if (CurrentAmmo == MaxAmmo)
CanReload = false;
}
IEnumerator Reload()
{
ReloadPressed = true;
CanShoot = false;
Anim.SetBool(ReloadAnimName, true);
Debug.Log("Reloading!");
yield return new WaitForSeconds(ReloadTime);
int ReloadAmount = MaxAmmo - CurrentAmmo;
if(MagCapacity >= MaxAmmo)
{
CurrentAmmo = MaxAmmo;
MagCapacity -= ReloadAmount;
}
else
{
CurrentAmmo = MagCapacity;
MagCapacity = 0;
}
Debug.Log("Done Reloading!");
Anim.SetBool(ReloadAnimName, false);
ReloadPressed= false;
yield return new WaitForSeconds(TimeAfterReload);
CanShoot = true;
}
private void Shoot()
{
CurrentAmmo --;
Debug.Log("Boom!");
var Bullet = Instantiate(BulletPrefab, BulletSpawnPos.position, BulletSpawnPos.rotation);
Bullet.GetComponent<Rigidbody>().velocity = BulletSpawnPos.forward * BulletSpeed;
RaycastHit hit;
if(Physics.Raycast(ShootPos.transform.position, ShootPos.transform.TransformDirection(Vector3.forward), out hit, RayDist, ShootAble))
{
//Debug.Log("Hit something!");
Destroy(Bullet, 0.5f);
}else
{
//Debug.Log("Hit nothing!");
Destroy(Bullet, 2.5f);
}
}
}
EDIT
How I could set a key code value from the old input system to another key code using the new input system, so basically I am asking how I can do this:
KeyCode _ShootKey = KeyCode.Mouse0;
And i want to do this with the new input so it would look something like this example:
KeyCode _ShootKey = controls.player.shoot.ReadValue();
So problem is, whenever I try and do this with the new input system I cannot because it keeps on giving me errors saying that I cannot do this. Would there be any method that I could use to get passed this but make sure that it still does the same thing?