I am making a laser beam attack, which is currently accessed by using (Input.GetMouseDown(KeyCode.F)), and I want to change it to use GetAxis(“Fire1”).
For some reason, a while loop does not run when using GetAxis instead of GetKey and I would be grateful if anyone could give an explanation as to why this is happening.
In my code, the lines 20, 30 and 52, are where I am tryng to access the firing coroutine.
Is there something unique about GetAxis that means it cannot be directly swapped with GetKey? I am using Unity 5.6.
Thanks for any help provided.
Script
void Update()
{
if(reloading)
{
reloadTimer += Time.deltaTime;
if(reloadTimer > reloadTimerMax)
{
reloadTimer = 0f;
shootTimer = 0f;
reloading = false;
shootSlider.value = 0;
spriteFill.color = Color.white;
}
}
//Make this work with the MouseDown/Fire1 Axis!!
if(Input.GetKeyDown(KeyCode.F))
// if(Input.GetAxis("Fire1") != 0)
{
if(!reloading)
{
StopCoroutine(ShootForward());
StartCoroutine(ShootForward());
}
}
if(!Input.GetKey(KeyCode.F))
// if(Input.GetAxis("Fire1) == 0)
{
if(!reloading)
{
if(shootTimer > 0)
{
shootTimer -= (Time.deltaTime / 3);
}
else
{
shootTimer = 0;
}
shootSlider.value = shootTimer;
}
}
}
IEnumerator ShootForward()
{
lr.enabled = true;
while(Input.GetKey(KeyCode.F) && shootTimer < shootTimerMax)
// if(Input.GetAxis("Fire1) != 0)
{
shootTimer += Time.deltaTime;
Ray ray = new Ray(firePosition.position, transform.forward);
RaycastHit hit;
lr.SetPosition(0, ray.origin);
if(Physics.Raycast(ray, out hit, 100))
{
lr.SetPosition(1, hit.point);
Vector3 hitPosition = lr.GetPosition(1);
hitParticle.transform.position = hitPosition;
hitParticle.SetActive(true);
}
else
{
lr.SetPosition(1, ray.GetPoint(100));
hitParticle.SetActive(false);
}
shootSlider.value = shootTimer;
yield return null;
}
if(shootTimer >= shootTimerMax)
{
reloading = true;
spriteFill.color = Color.red;
yield return null;
}
lr.enabled = false;
hitParticle.SetActive(false);
}