﻿using UnityEngine;
using System.Collections;

public class MouseForces : MonoBehaviour {

	public GameObject sphere;
	Camera cam;
	Quaternion look = Quaternion.identity;
	bool rotate = false;

	// Use this for initialization
	void Start () {
		cam = GetComponent<Camera> ();
	}
	
	// Update is called once per frame
	void Update () {
		RaycastHit hit;
		Ray ray = cam.ScreenPointToRay (Input.mousePosition);
		if (Physics.Raycast (ray, out hit)) {
			if (Input.GetMouseButtonUp (0)) {

				look = Quaternion.LookRotation (hit.point - sphere.transform.position);
				rotate = true;
				sphere.transform.LookAt (hit.point);

				Vector3 dir = hit.point - sphere.transform.position;
				sphere.GetComponent<Rigidbody> ().AddForce (dir * 20f);
			}
		}

//		if (Quaternion.Angle (sphere.transform.rotation, look) > 20f && rotate) {
//			sphere.transform.rotation = Quaternion.Lerp (sphere.transform.rotation, look, Time.deltaTime * 5f);
//		} else {
//			rotate = false;
//		}
	}
}
