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 Warl0ck · Oct 25, 2016 at 08:16 PM · mobilecharactercontrollerjoystickglitchvirtual

Character Controller Initial Problem With Mobile

I am having my mobile Joystick Programmed as below...

When I touch first joystick or use joystick at first time in mobile it stuck and character controller also get a bit glitch and then after first touch it goes very smooth as per other mobile joysticks..

where i have made mistake ...please someone try me to solve this

using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems;

public class Mobile_Joystick : MonoBehaviour,IDragHandler, IPointerUpHandler, IPointerDownHandler {

 //Public Variables for editior editing
 [Header("JOYSTICK SETTINGS:")]
 [Range (0,2.5f)]
 public float Stick_Move_Range;                    //Stick Movement Radius
 [Tooltip("Hide Joystick While Not Being Used.")]
 public bool Hide_On_Idle;                            //If Stick Should Be hidden when in idle state or not using or not touching
 [Tooltip("Alpha of Stick When Not Being Used.")]
 [Range (0,1f)]
 public float Alpha_Fade_Stick;                    //Alpha of Stick when Joystick is not being used
 [Tooltip("Alpha of Joystick Base When Not Being Used.")]
 [Range (0,1f)]
 public float Aplha_Fade_Joystick_Base;            //Alpha of Joystick base when Joystick is not being used









 //Private Variables
 private Image Joystick_Base;                 //Joystick base Image
 private Image Stick;                        //Stick Image



 //Some Vectors

 private bool Is_Touched;                                //Bool is touched or not
 private int Current_Finger;                            //Current Finger ID Which is Touching
 private Vector2 Touch_Position;                             //Store Current Position on Joystick
 private float IO_X;                                        //Gives Current Distance From Center.x To Finger Touch
 private float IO_Y;                                        //Gives Current Distance From Center.y To Finger Touch
 private Vector2 InputDirection;                        //Store Output
 private Vector2 Final_Input;                            //Stores Final Output 



 void Start(){
     
     Joystick_Base = GetComponent<Image>();               //getting image componnents
     Stick = transform.GetChild(0).GetComponent<Image>(); //this command is used because there is only one child in hierarchy
 }




 public void OnPointerDown(PointerEventData data){
     if (!Is_Touched) {
         Is_Touched=true;
         OnDrag (data);
         Current_Finger = data.pointerId;
     }
 }
 
 public void OnDrag(PointerEventData data){

     if (data.pointerId == Current_Finger && Is_Touched) {
         //To get InputDirection
         RectTransformUtility.ScreenPointToLocalPointInRectangle
         (Joystick_Base.rectTransform, 
          data.position,
          data.pressEventCamera,
          out Touch_Position);
     
         Touch_Position.x = (Touch_Position.x / Joystick_Base.rectTransform.sizeDelta.x);
         Touch_Position.y = (Touch_Position.y / Joystick_Base.rectTransform.sizeDelta.y);

         //As Per Pivot It Sets Values
         IO_X = (Joystick_Base.rectTransform.pivot.x == 1f) ? Touch_Position.x * 2 + 1 : Touch_Position.x * 2 - 1;
         IO_Y = (Joystick_Base.rectTransform.pivot.y == 1f) ? Touch_Position.y * 2 + 1 : Touch_Position.y * 2 - 1;

         //Normalize the Input For Our Joystick Movement
         InputDirection = new Vector2 (IO_X, IO_Y);
         InputDirection = (InputDirection.magnitude > 1) ? InputDirection.normalized : InputDirection;

     

         //to define the area in which joystick can move around
         Stick.rectTransform.anchoredPosition = new Vector2 (InputDirection.x * (Joystick_Base.rectTransform.sizeDelta.x / 3)*Stick_Move_Range,
                                                             InputDirection.y * (Joystick_Base.rectTransform.sizeDelta.y / 3)*Stick_Move_Range
                                                             );
     }
 }
 

 public void OnPointerUp(PointerEventData data){
     if (data.pointerId == Current_Finger) {
         Is_Touched = false;
         InputDirection = Vector2.zero;
         Final_Input = Vector2.zero;
         Stick.rectTransform.anchoredPosition = Vector2.zero;
     }

 }


 public Vector2 Joystick_Input()
 {
     //Our FInal Vector Which Holds Inputs
     Final_Input = new Vector2(InputDirection.x,InputDirection.y);
     return Final_Input;
 }

 

}

This gives me output and i am taking its output to my fps controller and its scripted like below

using UnityEngine; using System.Collections;

public class FPS_Controller : MonoBehaviour {

 [System.Serializable]
 public class Script_Resources{
     public Mobile_Joystick Joystick;
     public Mobile_Touchpad Touchpad;
     public Camera FPS_Camera;
 }
 public Script_Resources Resources;

 public float Speed;

 private Vector2 Joystick_IO;
 private Vector2 Touchpad_IO;


 private CharacterController cc;

 Vector3 move;

 void Start()
 {
     cc = GetComponent < CharacterController> ();
 }

 void Update(){



     Joystick_IO = Resources.Joystick.Joystick_Input ();      //takes input from another script


 

     cc.SimpleMove (Joystick_IO.x,0,Joystick_IO.y);     //apply it to character controller

 }






}

but it gives me initial glitch

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Use prefab mobile single stick control on WebGL build 2 Answers

USB input on Android Application 0 Answers

Make joystick rotate object 0 Answers

Mobile joystick is not working in unity5.6.1 only jump button works 2 Answers

Rigidbody rotation with virtual joystick, weird behaviour 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