Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Coding Begginer need help including- input.getbutton("Fire1")

Discussion in 'Getting Started' started by shauryavarshney272, May 23, 2022.

  1. shauryavarshney272

    shauryavarshney272

    Joined:
    Nov 8, 2021
    Posts:
    8
    I am a newbie with no coding experience just started using unity I was following brackeys Ray cast shooting video,
    , 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 which will autofire on holding.
    Thanks&sorry if this is a silly question




    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.VisualScripting;
    5.  
    6. public class Gun : MonoBehaviour
    7. {
    8.  
    9.     public float damage = 10f;
    10.     public float range = 100f;
    11.     public float fireRate = 15f;
    12.     public float impactForce = 30f;
    13.    
    14.  
    15.     public Camera fpsCam;
    16.     public ParticleSystem muzzleFlash;
    17.     public GameObject impactEffect;
    18.  
    19.     private float nextTimeToFire = 0f;
    20.  
    21.  
    22.  
    23.     // Update is called once per frame
    24.     public void Update()
    25.     {
    26.         if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
    27.         {
    28.             nextTimeToFire = Time.time + 1f / fireRate;
    29.             Shoot();
    30.         }
    31.  
    32.     }
    33.  
    34.     public void Shoot()
    35.     {
    36.         muzzleFlash.Play();
    37.  
    38.         RaycastHit hit;
    39.         if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
    40.         {
    41.            
    42.  
    43.             Enemy enemy = hit.transform.GetComponent<Enemy>();
    44.             if (enemy != null)
    45.             {
    46.                 enemy.TakeDamage(damage);
    47.             }
    48.  
    49.             if (hit.rigidbody != null)
    50.             {
    51.                 hit.rigidbody.AddForce(-hit.normal * impactForce);
    52.             }
    53.  
    54.             GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
    55.             Destroy(impactGO, 2f);    
    56.         }
    57.     }
    58. }
    59.  
     
  2. shauryavarshney272

    shauryavarshney272

    Joined:
    Nov 8, 2021
    Posts:
    8
    sorry if I reply late cuz I am Indian it is now 1:00 a.m. at night
     
  3. r31o

    r31o

    Joined:
    Jul 29, 2021
    Posts:
    460
    Make a UI button, and on the OnClick event assign your gun script and call shoot
     
  4. shauryavarshney272

    shauryavarshney272

    Joined:
    Nov 8, 2021
    Posts:
    8
    I did that but then i have to click it every time it is not autofirring
     
  5. r31o

    r31o

    Joined:
    Jul 29, 2021
    Posts:
    460
    You should use IPointerClickHandler and IPointerUpHandler interfaces, to assign a bool to check if the button is pressed or not.
     
    shauryavarshney272 likes this.
  6. shauryavarshney272

    shauryavarshney272

    Joined:
    Nov 8, 2021
    Posts:
    8
    But ipointer is for non ui game objects
     
  7. r31o

    r31o

    Joined:
    Jul 29, 2021
    Posts:
    460
    No, is for both.
    The unique requirment is that the gameObject needs a graphics raycaster, which UI elements have by default
     
  8. shauryavarshney272

    shauryavarshney272

    Joined:
    Nov 8, 2021
    Posts:
    8
    Can u show me how to implement it in the code I am not able to find tutorial on it
     
  9. r31o

    r31o

    Joined:
    Jul 29, 2021
    Posts:
    460