Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TotalPixel · Jul 24, 2017 at 07:21 PM · scripting problem

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);
         }
     }
 }

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ALG2132 · Jul 30, 2017 at 01:30 PM 0
Share

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).

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

111 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Question regarding Raycast hitting UI button object instead of gameobject 0 Answers

My UI Buttons stopped working 0 Answers

How to import a list or array of objects from a json file? 1 Answer

How can i make objects to move in random speed ? 1 Answer

(Localization System) Change Language Problem 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges