using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class orbita : MonoBehaviour
{

    // Start is called before the first frame update
    public Transform target;
    public float speed;
    public bool randomOrbit;
    Vector3 pos = new Vector3(0f,0f,0f);
    float delta = 0;
    float dist;
    float startPoint;
    void Start()
    {
        dist = Vector3.Distance(target.position, transform.position);

        if (randomOrbit)
        {
            startPoint = Random.Range(0, 360);
        }
        else
        {
            
            startPoint = Mathf.Atan2(gameObject.transform.position.x- target.position.x, gameObject.transform.position.z - target.position.z);
        }
    }

    // Update is called once per frame
    void Update()
    {

        delta += (Time.deltaTime * speed / 100);
       
            pos.x = target.position.x + dist * Mathf.Sin(delta + startPoint);
            pos.z = target.position.z + dist * Mathf.Cos(delta + startPoint);
        
           
        gameObject.transform.position = pos;
        
    }
}
