Advertisement
johnnygoodguy2000

PlayerShoot.cs

Jun 15th, 2024
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.38 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. using UnityEngine;
  5. using UnityEngine.InputSystem;
  6.  
  7. public class PlayerShoot : MonoBehaviour
  8. {
  9.     [SerializeField] GameObject bulletPrefab;
  10.  
  11.     [SerializeField] private float bulletSpeed;
  12.     [SerializeField] Transform gunOffset;
  13.     [SerializeField] float timeBetweenShots;
  14.  
  15.     private bool fireContinuously;
  16.     private bool fireSingle;
  17.  
  18.     private float lastFireTime;
  19.  
  20.  
  21.  
  22.  
  23.  
  24.    
  25.  
  26.     // Update is called once per frame
  27.     void Update()
  28.     {
  29.         if (fireContinuously || fireSingle)
  30.         {
  31.        
  32.             float timeSinceLastFire = Time.time - lastFireTime;
  33.  
  34.             if (timeSinceLastFire >= timeBetweenShots)
  35.  
  36.             FireBullet();
  37.             lastFireTime = Time.time;
  38.  
  39.             fireSingle = false;
  40.  
  41.  
  42.         }
  43.        
  44.        
  45.     }
  46.  
  47.     private void FireBullet()
  48.     {
  49.         GameObject bullet = Instantiate(bulletPrefab, gunOffset.position, transform.rotation);
  50.         Rigidbody2D rigidbody = bullet.GetComponent<Rigidbody2D>();
  51.         rigidbody.velocity = bulletSpeed * transform.up  ;
  52.     }
  53.     private void OnFire( InputValue inputValue )
  54.    
  55.        {
  56.         fireContinuously = inputValue.isPressed;
  57.  
  58.         if (inputValue.isPressed)
  59.         {
  60.             fireSingle = true;
  61.         }
  62.            
  63.        }
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement