Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
2
Question by cho_yangxuan · Feb 21, 2019 at 07:19 AM · joystickjoysticks

Joystick moves unexpectedly in Unity

Context: I am using the FixedJoystick in Joystick Pack by Fenerax Studios on Unity asset store in my project.

The problem is whenever I touch the joystick (without even moving it), the handle immediately shifts to the upper right corner and my model moves downwards as a result.

Below is the image illustrating joystick issue:

[1]: /storage/temp/133489-joystick-bug.png

The red cross in the image indicates where the user touches the joystick, the handle doesn't move there as expected but jumps upwards instead.

Here is the code:

 using UnityEngine;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;
 
 public class FixedJoystick : Joystick
 {
     Vector2 joystickPosition = Vector2.zero;
     public GameObject heldDownBlockRegion;
     private Camera cam = new Camera();
     private bool inARMode = true;
 
 
 
 void Start()
 {
     joystickPosition = RectTransformUtility.WorldToScreenPoint(cam, background.position);
     gameObject.SetActive(false);
 
 }
 
 
 public override void OnDrag(PointerEventData eventData)
 {
     Vector2 direction = eventData.position - joystickPosition;
     inputVector = (direction.magnitude > background.sizeDelta.x / 2f) ? direction.normalized : direction / (background.sizeDelta.x / 2f);
     ClampJoystick();
     handle.anchoredPosition = (inputVector * background.sizeDelta.x / 2f) * handleLimit;
 }
 
 public override void OnPointerDown(PointerEventData eventData)
 {
     heldDownBlockRegion.SetActive(true);
     OnDrag(eventData);
 }
 
 public override void OnPointerUp(PointerEventData eventData)
 {
     heldDownBlockRegion.SetActive(false);
     inputVector = Vector2.zero;
     handle.anchoredPosition = Vector2.zero;
 }
 
 public void changeToAR()
 {
     showJoyStick(false);
     inARMode = true;
 }
 
 public void changeToNonAR()
 {
     inARMode = false;
 }
 
 public void showJoyStick(bool action)
 {
     if (!inARMode)
     {
         if (action == false)
         {
             heldDownBlockRegion.SetActive(false);
             inputVector = Vector2.zero;
             handle.anchoredPosition = Vector2.zero;
         }
         gameObject.SetActive(action);
     }
 }
 }

I am pretty new to this and would really appreciate any help. Thanks!

joystick-bug.png (8.0 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by mauriciomb · Jul 02, 2020 at 06:09 PM

@cho_yangxuan I had this same problem but with the floating joystick. The first touch down would move my player object down left until release. I figured out a solution to simulate a pointer down and up at the Start method in the Joystick class. Here is the code:

     var pointer = new PointerEventData(EventSystem.current);
     ExecuteEvents.Execute(gameObject, pointer, ExecuteEvents.pointerDownHandler);
     ExecuteEvents.Execute(gameObject, pointer, ExecuteEvents.pointerUpHandler);
Comment
Add comment · Show 2 · 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
avatar image goodos_ · Jun 12, 2021 at 05:05 PM 0
Share

Thx u so much!

avatar image mohamedkaram09 · Jan 16 at 12:12 PM 0
Share

That's actually pretty smart. I had the same problem and tried everything to fix it from my code until I finally came to the conclusion that the joystick package itself is bugged for some reason (I may be wrong though)

anyways, your solution worked perfectly for me but with a $$anonymous$$or change since I had a problem with dragging not tapping. Here is the code for any future lost developers as myself!

  var pointer = new PointerEventData(EventSystem.current);
         ExecuteEvents.Execute(gameObject, pointer, ExecuteEvents.dragHandler);
         ExecuteEvents.Execute(gameObject, pointer, ExecuteEvents.pointerUpHandler);

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

103 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

Related Questions

PS4 controller right analog stick issue 3 Answers

Dual-Joystick Controlled 3rd-Person Orbit Camera 1 Answer

How to implement virtual moveable joystick? 2 Answers

Two Joystick controller not working together 3 Answers

Input Axis names and axis type (help with joysticks) 1 Answer


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