Click Button and Follow Mouse
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.
So you are trying to get it where it's drag and drop, similar to how you would do a card? Like click and hold and drag it somewhere?
@GA$$anonymous$$ZER0 It is not similar to drag and drop though. It's like if you clicked the left mouse button after releasing the button the object still sticks to the cursor, if I click again then it will drop the object.
Answer by GAMZER0 · Aug 31, 2015 at 04:31 AM
I hope this tutorial will help, it may not be exact. But it should point you in the right direction to how to go about it. https://www.youtube.com/watch?v=jOOdJZS987Y
Your answer
![](https://koobas.hobune.stream/wayback/20220612002047im_/https://answers.unity.com/themes/thub/images/avi.jpg)