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 ako_si_ea · Dec 05, 2017 at 02:01 PM · 2djoysticktouch controlsno errorstandardassetsmobile

Joystick freezes when new scene is loaded,Joystick freezes when loading a new scene

My virtual joystick for android mobile freezes when new scene is loaded

Here is the joystick script

/ This script is from the Unity 5 Standard Assets with edits from Devin Curry Search for changes tagged with the //DCURRY comment Watch the tutorial here: www.Devination.com */ using System; using UnityEngine; using UnityEngine.EventSystems;

namespace UnityStandardAssets.CrossPlatformInput {

 public class Joystick : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
 {
     private static bool VAExists;

     public enum AxisOption
     {


         // Options for which axes to use
         Both, // Use both
         OnlyHorizontal, // Only horizontal
         OnlyVertical // Only vertical
     }

     public int MovementRange = 100;
     public AxisOption axesToUse = AxisOption.Both; // The options for the axes that the still will use
     public string horizontalAxisName = "Horizontal"; // The name given to the horizontal axis for the cross platform input
     public string verticalAxisName = "Vertical"; // The name given to the vertical axis for the cross platform input

     Vector3 m_StartPos;
     bool m_UseX; // Toggle for using the x axis
     bool m_UseY; // Toggle for using the Y axis
     CrossPlatformInputManager.VirtualAxis m_HorizontalVirtualAxis; // Reference to the joystick in the cross platform input
     CrossPlatformInputManager.VirtualAxis m_VerticalVirtualAxis; // Reference to the joystick in the cross platform input

     void Start() //DCURRY: Changed to Start from OnEnable
     {
         m_StartPos = transform.position;
         CreateVirtualAxes();

         m_UseX = true;
         m_UseY = true;
     }

     void UpdateVirtualAxes(Vector3 value)
     {
         var delta = m_StartPos - value;
         delta.y = -delta.y;
         delta /= MovementRange;
         if (m_UseX)
         {
             m_HorizontalVirtualAxis.Update(-delta.x);
         }

         if (m_UseY)
         {
             m_VerticalVirtualAxis.Update(delta.y);
         }
     }

     void CreateVirtualAxes()
     {
         // set axes to use
         m_UseX = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyHorizontal);
         m_UseY = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyVertical);
         // create new axes based on axes to use
         if (m_UseX)
         {
             if (CrossPlatformInputManager.AxisExists(horizontalAxisName))
             {
                 CrossPlatformInputManager.UnRegisterVirtualAxis(horizontalAxisName);
             }
             m_HorizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName);
             CrossPlatformInputManager.RegisterVirtualAxis(m_HorizontalVirtualAxis);
         }
         if (m_UseY)
         {
             if (CrossPlatformInputManager.AxisExists(verticalAxisName))
             {
                 CrossPlatformInputManager.UnRegisterVirtualAxis(verticalAxisName);
             }
             m_VerticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(verticalAxisName);
             CrossPlatformInputManager.RegisterVirtualAxis(m_VerticalVirtualAxis);
         }
     }

     public void RemoveAxes()
     {
         CrossPlatformInputManager.UnRegisterVirtualAxis(horizontalAxisName);
         CrossPlatformInputManager.UnRegisterVirtualAxis(verticalAxisName);
     }
     public void OnDrag(PointerEventData data)
     {
         Vector3 newPos = Vector3.zero;

         if (m_UseX)
         {
             int delta = (int)(data.position.x - m_StartPos.x);
             //delta = Mathf.Clamp(delta, - MovementRange, MovementRange); //DCURRY: Dont want to clamp individual axis
             newPos.x = delta;
         }

         if (m_UseY)
         {
             int delta = (int)(data.position.y - m_StartPos.y);
             //delta = Mathf.Clamp(delta, -MovementRange, MovementRange); //DCURRY: Dont want to clamp individual axis
             newPos.y = delta;
         }
         //DCURRY: ClampMagnitude to clamp in a circle instead of a square
         transform.position = Vector3.ClampMagnitude(new Vector3(newPos.x, newPos.y, newPos.z), MovementRange) + m_StartPos;
         UpdateVirtualAxes(transform.position);
     }


     public void OnPointerUp(PointerEventData data)
     {
         transform.position = m_StartPos;
         UpdateVirtualAxes(m_StartPos);
     }


     public void OnPointerDown(PointerEventData data) { }

     void OnDisable()
     {
         // remove the joysticks from the cross platform input
         if (m_UseX)
         {
             m_HorizontalVirtualAxis.Remove();
         }
         if (m_UseY)
         {
             m_VerticalVirtualAxis.Remove();
         }
     }
 }
 

},The joystick for mobile freezes when I go to another scene. Please help.

Here's the code for my Joystick:

/ This script is from the Unity 5 Standard Assets with edits from Devin Curry Search for changes tagged with the //DCURRY comment Watch the tutorial here: www.Devination.com */ using System; using UnityEngine; using UnityEngine.EventSystems;

namespace UnityStandardAssets.CrossPlatformInput {

 public class Joystick : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
 {
     private static bool VAExists;

     public enum AxisOption
     {


         // Options for which axes to use
         Both, // Use both
         OnlyHorizontal, // Only horizontal
         OnlyVertical // Only vertical
     }

     public int MovementRange = 100;
     public AxisOption axesToUse = AxisOption.Both; // The options for the axes that the still will use
     public string horizontalAxisName = "Horizontal"; // The name given to the horizontal axis for the cross platform input
     public string verticalAxisName = "Vertical"; // The name given to the vertical axis for the cross platform input

     Vector3 m_StartPos;
     bool m_UseX; // Toggle for using the x axis
     bool m_UseY; // Toggle for using the Y axis
     CrossPlatformInputManager.VirtualAxis m_HorizontalVirtualAxis; // Reference to the joystick in the cross platform input
     CrossPlatformInputManager.VirtualAxis m_VerticalVirtualAxis; // Reference to the joystick in the cross platform input

     void Start() //DCURRY: Changed to Start from OnEnable
     {
         m_StartPos = transform.position;
         CreateVirtualAxes();

         m_UseX = true;
         m_UseY = true;
     }

     void UpdateVirtualAxes(Vector3 value)
     {
         var delta = m_StartPos - value;
         delta.y = -delta.y;
         delta /= MovementRange;
         if (m_UseX)
         {
             m_HorizontalVirtualAxis.Update(-delta.x);
         }

         if (m_UseY)
         {
             m_VerticalVirtualAxis.Update(delta.y);
         }
     }

     void CreateVirtualAxes()
     {
         // set axes to use
         m_UseX = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyHorizontal);
         m_UseY = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyVertical);
         // create new axes based on axes to use
         if (m_UseX)
         {
             if (CrossPlatformInputManager.AxisExists(horizontalAxisName))
             {
                 CrossPlatformInputManager.UnRegisterVirtualAxis(horizontalAxisName);
             }
             m_HorizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName);
             CrossPlatformInputManager.RegisterVirtualAxis(m_HorizontalVirtualAxis);
         }
         if (m_UseY)
         {
             if (CrossPlatformInputManager.AxisExists(verticalAxisName))
             {
                 CrossPlatformInputManager.UnRegisterVirtualAxis(verticalAxisName);
             }
             m_VerticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(verticalAxisName);
             CrossPlatformInputManager.RegisterVirtualAxis(m_VerticalVirtualAxis);
         }
     }

     public void RemoveAxes()
     {
         CrossPlatformInputManager.UnRegisterVirtualAxis(horizontalAxisName);
         CrossPlatformInputManager.UnRegisterVirtualAxis(verticalAxisName);
     }
     public void OnDrag(PointerEventData data)
     {
         Vector3 newPos = Vector3.zero;

         if (m_UseX)
         {
             int delta = (int)(data.position.x - m_StartPos.x);
             //delta = Mathf.Clamp(delta, - MovementRange, MovementRange); //DCURRY: Dont want to clamp individual axis
             newPos.x = delta;
         }

         if (m_UseY)
         {
             int delta = (int)(data.position.y - m_StartPos.y);
             //delta = Mathf.Clamp(delta, -MovementRange, MovementRange); //DCURRY: Dont want to clamp individual axis
             newPos.y = delta;
         }
         //DCURRY: ClampMagnitude to clamp in a circle instead of a square
         transform.position = Vector3.ClampMagnitude(new Vector3(newPos.x, newPos.y, newPos.z), MovementRange) + m_StartPos;
         UpdateVirtualAxes(transform.position);
     }


     public void OnPointerUp(PointerEventData data)
     {
         transform.position = m_StartPos;
         UpdateVirtualAxes(m_StartPos);
     }


     public void OnPointerDown(PointerEventData data) { }

     void OnDisable()
     {
         // remove the joysticks from the cross platform input
         if (m_UseX)
         {
             m_HorizontalVirtualAxis.Remove();
         }
         if (m_UseY)
         {
             m_VerticalVirtualAxis.Remove();
         }
     }
 }
 

}

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

183 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 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

Error : The second touch should not work at all. How do I fix this error?,Error: The second touch should not work at all. How do I fix this error? 0 Answers

Detect a Full 360 input on a joystick 1 Answer

Error CS1026: Unexpected symbol ;, expecting ) 0 Answers

OnScreen-Stick that moves to the position touched 0 Answers

How can i move a 2d player in a 3d scene in x and z axis? maybe with a canvas joystick? 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