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

public class commonpower : MonoBehaviour {

    private GameObject target;
    public GameObject projectilePrefab;
    private Rigidbody2D rb;
    public float speed;

    void start()
    {
        rb = projectilePrefab.GetComponent<Rigidbody2D>();
    }

    public void OnFire()
    {
        Instantiate(projectilePrefab, transform.position, transform.rotation);
        target = GameObject.FindWithTag("Player");
        float distance = Vector2.Distance(projectilePrefab.transform.position, target.transform.position);

        if (distance >= 2 || distance <= 15)
        {
            Vector2 direction = target.transform.position - rb.transform.position;

            direction.Normalize();

            Vector3.Cross(direction, rb.transform.up);

            rb.velocity = rb.transform.up * speed;
        }

        else
        {
            
        }
    }
}