- Home /
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