Hi I got this problem with my script, what I want is to select the object when I click(leftmousebutton) on it then it will follow the mouse cursor. AND if I click(leftmousebutton) it again It will stop following the mouse cursor. I already got the first one working.
My problem now is on how to STOP the object in following the mouse cursor using the same leftmouseclick.
NOTE: I actually already got the object stop following the mouse cursor, but I cant click on the object again for him to follow the mouse cursor.
try it so you can see it your self before answering It might help you see the actual problem.
(Create a C# script and paste this Code then put this on the object you follow the mouse cursor) ![]()
using UnityEngine;
using System.Collections;
public class Follow : MonoBehaviour {
public float depth = 10.0f;
public enum State
{
selected,
notselected
}
public State state;
public bool Click = false;
void Start ()
{
state = Follow.State.notselected;
}
void Update ()
{
if (Click == true)
{
Screen.showCursor = true;
Vector3 mousePos = Input.mousePosition;
Vector3 wantedPos = Camera.main.ScreenToWorldPoint
(new Vector3 (mousePos.x, mousePos.y, depth));
transform.position = wantedPos;
}
}
public void OnMouseUp()
{
if(state == Follow.State.notselected)
{
Selected();
state = Follow.State.selected;
}
}
public void OnMouseDown()
{
if(state == Follow.State.selected)
{
NotSelected();
state = Follow.State.notselected;
}
}
private void Selected()
{
Click = true;
}
private void NotSelected()
{
Click = false;
}
}