- Home /
In game menu
Hi all, im trying to make an in game menu to select levels.
I have a drag camera script to move left or right through the menu and each level sprite has a collider attached for when it is clicked. This all works fine if you click on the scene and drag the menu and then click the level you want. However if I want to drag the menu but accidentally click a level it registers the click before I drag the menu.
Does anyone have any idea of how I can stop it automatically clicking on the menu item when I want to drag the whole menu?.
Any help appreciated.
This is the drag script
using UnityEngine;
using System.Collections;
// Attach to orthographic camera with rotation (0,0,0)
public class DragCamera1 : MonoBehaviour
{
private Vector3 startMousePos;
private float camerapositionx;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
PlayerPrefs.SetInt("MouseDragged", 1);
startMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
startMousePos.z = 0.0f;
startMousePos.y = 0.0f;
}
if (Input.GetMouseButton(0))
{
Vector3 nowMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
nowMousePos.z = 0.0f;
nowMousePos.y = 0.0f;
transform.position += startMousePos - nowMousePos;
camerapositionx = transform.position.x;
if (camerapositionx < 0.0f)
{
transform.position = new Vector3(0.0f,0.0f,-12.0f);
}
}
if (Input.GetMouseButtonUp(0))
{
PlayerPrefs.SetInt("MouseDragged", 0);
Debug.Log("mouse up");
}
}
}
and this is the objects click script
#pragma strict
var activevehicle1 : GameObject;
function Start ()
{
}
function Update () {
}
function OnMouseDown ()
{
if (Input.GetKey ("mouse 0"))
{
if(PlayerPrefs.GetInt("MouseDragged")==0)
{
PlayerPrefs.SetInt("MakeActiveLevel",1);
activevehicle1.SetActive(false);
}
}
}
First of all, the following is not needed..
function Start ()
{
}
function Update () {
}
I'll have a look at your code and get back to you with an answer (I hope).
Answer by TheSOULDev · Jul 30, 2017 at 11:17 PM
From what I understand, you want your script not to enter a level when you drag, but you want it to work on click?
I could suggest making a boolean variable that would be set on true OnMouseDown. Then, make an OnDrag event which would set that boolean to false, and an OnMouseUp event that would do the click if the initial boolean is still true.
Now, you could create some sort of system that wouldn't disable the click on short drags (as OnDrag is very sensitive). In that case, you should implement some sort of mouse movement derivation in OnDrag. You would have a variable where your old position is stored. If the difference between that old position and the current position was greater than some set constant, you would set the initial boolean to false, otherwise you'd keep it true.
Overall, use Event Triggers and an Event System that use boolean values which you can use to differentiate whether the user wanted to click, or to drag, and to click only if the user didn't drag too much.
For more info on Unity's Event Triggers, check here: https://docs.unity3d.com/ScriptReference/EventSystems.EventTrigger.html For more info on Unity's Event System, check here: https://docs.unity3d.com/ScriptReference/EventSystems.EventSystem.html