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

public class AppleTree : MonoBehaviour
{
    public GameObject applePrefab;
    public float speed = 1.0f;
    public float edge = 16.0f;
    public float chanceToChange = 0.01f;
    // Start is called before the first frame update
    void Start()
    {
        InvokeRepeating("DropApple", 1f, 1.0f);
    }

    void DropApple()
    {
        Vector3 pos = transform.position;
        pos.z += 2.0f;
        pos.y -= 2.0f;
        GameObject apple = Instantiate(applePrefab, pos, Quaternion.identity);
        Destroy(apple, 2.0f);

    }

    // Update is called once per frame
    void Update()
    {
        transform.Translate(Vector3.right * speed * Time.deltaTime);
        if (transform.position.x < -edge || transform.position.x > edge){
            speed *= -1;
        }
    }

    void FixedUpdate()
    {
        if(chanceToChange > Random.value) speed *= -1;
    }
}
