Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using System.Runtime.CompilerServices;
- using UnityEngine;
- using UnityEngine.InputSystem;
- public class PlayerShoot : MonoBehaviour
- {
- [SerializeField] GameObject bulletPrefab;
- [SerializeField] private float bulletSpeed;
- [SerializeField] Transform gunOffset;
- [SerializeField] float timeBetweenShots;
- private bool fireContinuously;
- private bool fireSingle;
- private float lastFireTime;
- // Update is called once per frame
- void Update()
- {
- if (fireContinuously || fireSingle)
- {
- float timeSinceLastFire = Time.time - lastFireTime;
- if (timeSinceLastFire >= timeBetweenShots)
- FireBullet();
- lastFireTime = Time.time;
- fireSingle = false;
- }
- }
- private void FireBullet()
- {
- GameObject bullet = Instantiate(bulletPrefab, gunOffset.position, transform.rotation);
- Rigidbody2D rigidbody = bullet.GetComponent<Rigidbody2D>();
- rigidbody.velocity = bulletSpeed * transform.up ;
- }
- private void OnFire( InputValue inputValue )
- {
- fireContinuously = inputValue.isPressed;
- if (inputValue.isPressed)
- {
- fireSingle = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement