Skip to content

Instantly share code, notes, and snippets.

@ghostofgamer
Last active October 1, 2023 06:32
Show Gist options
  • Save ghostofgamer/2a4528f38b0b067d2f11a33f76334c5c to your computer and use it in GitHub Desktop.
Save ghostofgamer/2a4528f38b0b067d2f11a33f76334c5c to your computer and use it in GitHub Desktop.
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