Home > Blockchain >  Unity - help needed to touch and hold UI button to call update method and auto fire (here is the scr
Unity - help needed to touch and hold UI button to call update method and auto fire (here is the scr

Time:05-26

I am a newbie with no coding experience just started using unity I was following brackeys Ray cast shooting video,https://youtu.be/THnivyG0Mvo,, and I created the gun script but I wanted to turn it into mobile so I wanted to autofire by holding a UI button. plz, I need help me know no code. this is my code. I was thinking is there a way to put something in place of input.getbutton to make a button that will autofire on holding. Thanks & sorry if this is a silly question

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.VisualScripting;
public class Gun : MonoBehaviour
{
public float damage = 10f;
public float range = 100f;
public float fireRate = 15f;
public float impactForce = 30f;

public Camera fpsCam;
public ParticleSystem muzzleFlash;
public GameObject impactEffect;
private float nextTimeToFire = 0f;
// Update is called once per frame
public void Update()
   {
    if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
    {
        nextTimeToFire = Time.time   1f / fireRate;
        Shoot();
    }
  }
  public void Shoot()
  {
    muzzleFlash.Play();
    RaycastHit hit;
    if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, 
        range))
    {
     
        Enemy enemy = hit.transform.GetComponent<Enemy>();
        if (enemy != null)
        {
            enemy.TakeDamage(damage);
        }
        if (hit.rigidbody != null)
        {
            hit.rigidbody.AddForce(-hit.normal * impactForce);
        }
        GameObject impactGO = Instantiate(impactEffect, hit.point, 
      Quaternion.LookRotation(hit.normal));
        Destroy(impactGO, 2f);  
    }
  }
}

CodePudding user response:

Create a Button, and add a new script to it. This script will be responsible for handling the button state. Essentially what we need to do is.. When OnPointerDown happens, set a value to indicate the button is held down. When OnPointerUp happens, reset the value.

using UnityEngine;
using UnityEngine.EventSystems;

public class ClickAndHoldButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerEnterHandler
{
    public bool IsHeldDown => isHeldDown;
    private bool isHeldDown;

    public void OnPointerDown(PointerEventData eventData)
    {
        isHeldDown = true;
    }
    public void OnPointerUp(PointerEventData eventData)
    {
        isHeldDown = false;
    }
    public void OnPointerEnter(PointerEventData eventData)
    {
    
    }
}

Then in Update of your gun, you will check the button state to see if it is being held down.

public ClickAndHoldButton mobileFireButton;

void Update()
{
    if (Time.time >= nextTimeToFire && (
        Input.GetButton("Fire1") ||
        mobileFireButton.IsHeldDown))
    {
        nextTimeToFire = Time.time   1f / fireRate;
        Shoot();
    }
}
  • Related