using UnityEngine;
using System.Collections;

public class ExecuteSpell : MonoBehaviour {

	public float speed = 70;
	
	public AudioClip SpellSound;
	public GameObject SpellPrefab;
	
	
	// Use this for initialization
	void Start () 
	{
	}
	
	// Update is called once per frame
	void Update () 
	{
		if(Input.GetKeyUp("f"))
			startSpell();
	
	}
	
	void startSpell()
	{
		//cast ray in camera view direction
		Vector3 screencenter = new Vector3(Screen.width/2,Screen.height/2, Camera.main.nearClipPlane);
		Ray ray = Camera.main.ScreenPointToRay(screencenter);
		Vector3 spellPosition = ray.origin + ray.direction * 3;
		
		//spawn and launch projectile
		GameObject fireball = Instantiate(SpellPrefab, spellPosition, transform.rotation) as GameObject;
		audio.PlayOneShot(SpellSound, 0.6f);
		
		Rigidbody rb = fireball.GetComponent<Rigidbody>();
		rb.useGravity = false;
		rb.AddForce(ray.direction * speed);
	}
}
