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 /
  • Help Room /
avatar image
0
Question by muhammadtahiriqbal · Feb 19, 2016 at 04:44 PM · c#uiunity5eventsystem

how to do on single click fire and on drag appears the joystick and get the input from joystick

i have a joystick in my scene

  1. on drag i want to do that take the input from joystick

  2. on single tap fire i try on dragg(event data) and on pointer down but when i single click,the on drag function is called. here is my code public virtual void OnDrag(PointerEventData eventData) {

          if (HideOnRelease)
             {
                 Hide(false);
             }
             // Unity remote multitouch related thing
             // When we feed fake PointerEventData we can't really provide a camera, 
             // it has a lot of private setters via not created objects, so even the Reflection magic won't help a lot here
             // Instead, we just provide an actual event camera as a public property so we can easily set it in the Input Helper class
             CurrentEventCamera = eventData.pressEventCamera ?? CurrentEventCamera;
    
             // We get the local position of the joystick
             Vector3 worldJoystickPosition;
             RectTransformUtility.ScreenPointToWorldPointInRectangle(_stickTransform, eventData.position,
                 CurrentEventCamera, out worldJoystickPosition);
    
             // Then we change it's actual position so it snaps to the user's finger
             _stickTransform.position = worldJoystickPosition;
             // We then query it's anchored position. It's calculated internally and quite tricky to do from scratch here in C#
             var stickAnchoredPosition = _stickTransform.anchoredPosition;
    
             // Some bitwise logic for constraining the joystick along one of the axis
             // If the "Both" option was selected, non of these two checks will yield "true"
             if ((JoystickMoveAxis & ControlMovementDirection.Horizontal) == 0)
             {
    
                 stickAnchoredPosition.x = _intermediateStickPosition.x;
             }
             if ((JoystickMoveAxis & ControlMovementDirection.Vertical) == 0)
             {
                 stickAnchoredPosition.y = _intermediateStickPosition.y;
             }
    
             _stickTransform.anchoredPosition = stickAnchoredPosition;
    
             // Find current difference between the previous central point of the joystick and it's current position
             Vector2 difference = new Vector2(stickAnchoredPosition.x, stickAnchoredPosition.y) - _intermediateStickPosition;
    
             // Normalisation stuff
             var diffMagnitude = difference.magnitude;
             var normalizedDifference = difference / diffMagnitude;
    
             // If the joystick is being dragged outside of it's range
             if (diffMagnitude > MovementRange)
             {
    
                 if (MoveBase && SnapsToFinger)
                 {
                     // We move the base so it maps the new joystick center position
                     var baseMovementDifference = difference.magnitude - MovementRange;
                     var addition = normalizedDifference * baseMovementDifference;
                     _baseTransform.anchoredPosition += addition;
                     _intermediateStickPosition += addition;
                 }
                 else
                 {
                     _stickTransform.anchoredPosition = _intermediateStickPosition + normalizedDifference * MovementRange;
                 }
             }
    
             // We don't need any values that are greater than 1 or less than -1
             var horizontalValue = Mathf.Clamp(difference.x * _oneOverMovementRange, -1f, 1f);
             var verticalValue = Mathf.Clamp(difference.y * _oneOverMovementRange, -1f, 1f);
             // Finally, we update our virtual axis
             HorizintalAxis.Value = horizontalValue;
             VerticalAxis.Value = verticalValue;
    
         }
    
         public void OnPointerUp(PointerEventData eventData)
         {
             // When we lift our finger, we reset everything to the initial state
             _baseTransform.anchoredPosition = _initialBasePosition;
             _stickTransform.anchoredPosition = _initialStickPosition;
             _intermediateStickPosition = _initialStickPosition;
    
             HorizintalAxis.Value = VerticalAxis.Value = 0f;
    
             print ("pointer is up");
    
             // We also hide it if we specified that behaviour
             if (HideOnRelease)
             {
                 Hide(true);
             }
         }
    
         public void OnPointerClick(PointerEventData eventData){
    
             print("on pointer click");
    
         }
         public void OnPointerDown(PointerEventData eventData)
         {
     
    

// fire here
//
// } // We also want to show it if we specified that behaviour // if (HideOnRelease) // { // Hide(false); // } }

     /// <summary>
     /// Simple "Hide" behaviour
     /// </summary>
     /// <param name="isHidden">Whether the joystick should be hidden</param>
     private void Hide(bool isHidden)
     {
         JoystickBase.gameObject.SetActive(!isHidden);
         Stick.gameObject.SetActive(!isHidden);
     }
 }

}

Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to send event to my canvas via script 0 Answers

Need alternative for onEndEdit on InputField 2 Answers

Error upon rebooting Unity - "Associated script cannot be loaded" How can I resolve this? 2 Answers

MobileSingleStickControl multitouch not supported. How fix? 0 Answers

My TakeDamage function doesnt work 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