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

public class Minion : MonoBehaviour
{
    public float moveSpeed = 1;
    public float turnSpeed = 0.1f;

    private Vector3 target;
    private Rigidbody rb;

    private void Awake()
    {
        rb = GetComponent<Rigidbody>();
        changeDirection();
        InvokeRepeating("changeDirection", 0, 1);
    }

    void FixedUpdate()
    {
        rb.AddForce(transform.forward * moveSpeed);
        rb.MoveRotation(Quaternion.RotateTowards(rb.rotation, Quaternion.LookRotation(target - transform.position, Vector3.up), turnSpeed));
    }

    private void changeDirection()
    {
        target = new Vector3(Random.Range(-8, 8), transform.position.y, Random.Range(-8, 8));
    }

    private void OnCollisionEnter(Collision collision)
    {
        changeDirection();
    }

}
