Hey! I am wondering how do detect when a controller button is held down, I want to rapid-fire a gun if it is. Any answers?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour
{
public float runSpeed = 5f;
public float shiftSpeed = 10f;
PlayerControls controls;
public GunScript shoot;
public BoxCreator cubeSpawn;
public GrenadeThrower throwGrenade;
Vector2 move;
Vector2 rotate;
Vector2 sprint;
void Awake()
{
controls = new PlayerControls();
controls.Gameplay.Movement.performed += ctx => move = ctx.ReadValue<Vector2>();
controls.Gameplay.Movement.canceled += ctx => move = Vector2.zero;
controls.Gameplay.Rotation.performed += ctx => rotate = ctx.ReadValue<Vector2>();
controls.Gameplay.Rotation.canceled += ctx => rotate = Vector2.zero;
controls.Gameplay.Shoot.performed += ctx => Shoot();
controls.Gameplay.SpawnCube.performed += ctx => SpawnCube();
controls.Gameplay.Grenade.performed += ctx => ThrowGrenade();
controls.Gameplay.Sprint.performed += ctx => Sprint();
controls.Gameplay.Sprint.canceled += ctx => NoSprint();
}
void Update()
{
transform.Translate(new Vector3(runSpeed * Input.GetAxis("Horizontal") * Time.deltaTime, 0f, runSpeed * Input.GetAxis("Vertical") * Time.deltaTime), Space.Self);
Vector2 r = new Vector2(-rotate.y, rotate.x) * 100f * Time.deltaTime;
transform.Rotate(r, Space.Self);
}
void OnEnable()
{
controls.Gameplay.Enable();
}
void OnDisable()
{
controls.Gameplay.Disable();
}
void Shoot()
{
if (shoot.currentAmmo <= 0)
{
return;
}
shoot.Shoot();
shoot.currentAmmo--;
}
void SpawnCube()
{
cubeSpawn.SpawnCube();
}
void ThrowGrenade()
{
if (throwGrenade.grenadeAmount <= 0f)
{
return;
}
throwGrenade.ThrowGrenade();
throwGrenade.grenadeAmount--;
}
void Sprint()
{
runSpeed = shiftSpeed;
}
void NoSprint()
{
runSpeed = 7f;
}
}