So I have this code, if I start the game the code that I put in the button makes the button follow the mouse. What I want is if I click that certain button that is when that button follows the cursor because I would implement it to my click and drop function. I have tried putting the code on the OnClick() function of the button but what it does is whenever I click on the button it follows the point where my mouse was clicked. So here is the code.
using UnityEngine;
using System.Collections;
public class FollowMouse : MonoBehaviour {
// Use this for initialization
public void Start ()
{
}
// Update is called once per frame
public void Update()
{
float distance = 1.0f;
bool useInitialCameraDistance = true;
float actualDistance;
if (useInitialCameraDistance) {
Vector3 toObjectVector = transform.position - Camera.main.transform.position;
Vector3 linearDistanceVector = Vector3.Project (toObjectVector, Camera.main.transform.forward);
actualDistance = linearDistanceVector.magnitude;
} else {
actualDistance = distance;
}
Vector3 mousePosition = Input.mousePosition;
mousePosition.z = actualDistance;
transform.position = Camera.main.ScreenToWorldPoint (mousePosition);
}
}
Thank you in advance.