Untitled

mail@pastecode.io avatar
unknown
csharp
3 years ago
1.9 kB
2
Indexable
Never
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Telekinesi : MonoBehaviour
{
    public List<Transform> Transformers = new List<Transform>();
    public List<GameObject> Pieces = new List<GameObject>();
    public float radius;
    public int limit = 10;

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Collider[] colliders = Physics.OverlapSphere(transform.position, radius);
            foreach (Collider col in colliders)
            {
                if (col.transform.tag == "Brick" && Pieces.Count < limit)
                {
                    col.gameObject.tag = "Untagged";
                    Pieces.Add(col.gameObject);
                }
            }

            StartCoroutine(Building());
        }
    }

    IEnumerator Building()
    {
        GameObject o = Pieces[Random.Range(0, Pieces.Count - 1)].gameObject;
        float duration = 0.25f;
        float timeElapsed = 0;
        float valueLerp = 0;

        int t = 0;

        while(Transformers.Count > 0)
        {
            valueLerp = Mathf.Lerp(0, 1, timeElapsed / duration);
            timeElapsed += Time.deltaTime;

            o.transform.position = Vector3.Lerp(o.transform.position, Transformers[t].transform.position, timeElapsed / duration);
            o.transform.rotation = Quaternion.Lerp(o.transform.rotation, Transformers[t].transform.rotation, timeElapsed / duration);

            if (timeElapsed > duration)
            {
                timeElapsed = 0;
                valueLerp = 0;
                Pieces.Remove(o);
                Transformers.Remove(Transformers[t]);
                t = Random.Range(0, Transformers.Count - 1);
                o = Pieces[Random.Range(0, Pieces.Count - 1)].gameObject;
            }

            yield return null;
        }
    }
}