Skip to content

Instantly share code, notes, and snippets.

@talatari
Forked from DmitriyProkopyev/BadExample.cs
Last active November 14, 2023 07:45
Show Gist options
  • Save talatari/d6449859ffec12afe5e0fe03d7a01484 to your computer and use it in GitHub Desktop.
Save talatari/d6449859ffec12afe5e0fe03d7a01484 to your computer and use it in GitHub Desktop.
Code Style Task. Проведите рефакторинг кода, исправьте все ошибки в именовании и форматировании так, чтобы не изменить принцип работы кода
using System.Collections;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class InstantiateBulletsShooting : MonoBehaviour
{
[SerializeField] private float _multiplier;
[SerializeField] private Rigidbody _prefabBullet;
[SerializeField] private Transform _objectToShoot;
private Coroutine _coroutineShooting;
private void Start() => _coroutineShooting = StartCoroutine(ShootingWorker());
private void OnDisable() => StopCoroutine(_coroutineShooting);
private IEnumerator ShootingWorker()
{
bool isWork = true;
float timeWaitShooting = 3f;
while (isWork)
{
Vector3 vector3Direction = (_objectToShoot.position - transform.position).normalized;
Rigidbody newBullet = Instantiate(_prefabBullet, transform.position + vector3Direction, Quaternion.identity);
newBullet.transform.up = vector3Direction;
newBullet.velocity = vector3Direction * _multiplier;
yield return new WaitForSeconds(timeWaitShooting);
}
}
}
using UnityEngine;
public class TargetMover : MonoBehaviour
{
[SerializeField] private Transform[] _allPoints;
[SerializeField] private Transform _target;
[SerializeField] private float _speed;
private int _index;
private void Start() => Initialization();
private void Update()
{
Transform target = _allPoints[_index];
transform.position = Vector3.MoveTowards(transform.position, target.position, _speed * Time.deltaTime);
if (transform.position == target.position)
{
SelectNextTarget();
}
}
private void Initialization()
{
_allPoints = new Transform[_target.childCount];
for (int i = 0; i < _target.childCount; i++)
{
if (_target.GetChild(i).TryGetComponent(out Transform transform))
{
_allPoints[i] = transform;
}
}
}
private void SelectNextTarget()
{
_index++;
if (_index == _allPoints.Length)
{
_index = 0;
}
Vector3 thisPointVector = _allPoints[_index].transform.position;
transform.forward = thisPointVector - transform.position;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment