Last active
October 1, 2023 06:32
-
-
Save ghostofgamer/2a4528f38b0b067d2f11a33f76334c5c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
[RequireComponent(typeof(Rigidbody))] | |
public class BulletMovement : MonoBehaviour | |
{ | |
[SerializeField] private Rigidbody _prefab; | |
[SerializeField] private float _speed; | |
[SerializeField] private float _timeWaitShooting; | |
[SerializeField] private Transform _objectToShoot; | |
private Coroutine _coroutine; | |
private void Start() | |
{ | |
if (_coroutine != null) | |
StopCoroutine(_coroutine); | |
_coroutine = StartCoroutine(ShootingWorker()); | |
} | |
private IEnumerator ShootingWorker() | |
{ | |
var waitForSeconds = new WaitForSeconds(_timeWaitShooting); | |
while (true) | |
{ | |
var direction = (_objectToShoot.position - transform.position).normalized; | |
var newBullet = Instantiate(_prefab, transform.position + direction, Quaternion.identity); | |
newBullet.transform.up = direction; | |
newBullet.velocity = direction * _speed; | |
yield return waitForSeconds; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment