using UnityEngine;
using System.Collections;

public class TractorBeamGun : MonoBehaviour
{
    public Rigidbody m_rigidBody;
    public Transform m_targetTransform;
    public GameObject m_tractorPrefab;
    public float k;
    public float c;
    public Vector3 m_rigidbodyOffset;


    private GameObject m_attachObj;
	// Use this for initialization
	void Start() 
    {
	    
	}
	
	// Update is called once per frame
	void Update() 
    {
        if (Time.realtimeSinceStartup < 1.0f)
            return;
        if (Input.GetMouseButtonDown(0))
        {
            m_attachObj = (GameObject)Instantiate(m_tractorPrefab, m_targetTransform.position, Quaternion.identity);
            m_attachObj.hingeJoint.connectedBody = m_rigidBody;
    

//             m_attachObj = new GameObject();
//             m_attachObj.transform.position = m_targetTransform.position;
//             TractorBeam beam = m_attachObj.AddComponent<TractorBeam>();
//             beam.connectedBody = m_rigidBody;
//             beam.k = k;
//             beam.c = c;
//             beam.offset = m_rigidbodyOffset;
        }
        else if (Input.GetMouseButtonUp(0))
        {
            Destroy(m_attachObj);
        }
        else if (Input.GetMouseButton(0))
        {
        }
	}

    void OnDrawGizmos()
    {
        if(m_attachObj)
        {
            Gizmos.color = Color.blue;
            Gizmos.DrawSphere(m_attachObj.transform.position, 1);
            Gizmos.color = Color.red;
            Gizmos.DrawLine(m_attachObj.transform.position, m_rigidBody.transform.position);
        }
    }
}
