aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Assets/Scripts/Spotlight.cs
blob: 95a0f5e4e3bc81bcebd5ac8a2b3d785b9341c56a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using System;
using System.Collections;
using UnityEngine;
using Random = UnityEngine.Random;

public class Spotlight : MonoBehaviour
{
  [SerializeField] private int duration;
  [SerializeField] private float speed;
  private Vector2 targetPosition;

  private void Start()
  {
    StartCoroutine(RandomMove());
  }

  private void FixedUpdate()
  {
    var target = new Vector3(targetPosition.x, targetPosition.y, transform.position.z);
    if (transform.position == Vector3.zero)
      transform.position = target;
    else
      transform.position = Vector3.MoveTowards(transform.position, target, Time.deltaTime * speed);
  }

  private IEnumerator RandomMove()
  {
    for (var i = 0; i < duration; i++)
    {
      targetPosition = new Vector2(Random.Range(-12f, 12f), Random.Range(3.5f, 6.2f));
      yield return new WaitForSeconds(.8f);
    }
    Destroy(gameObject);
  }
}