i got issue in on screen joystick unity c#

Asked by Alpesh savaliya

0
down vote
favorite
i m getting issue in joystick. when i click on joystick anywhere in joystick area its by default click at bottom then work fine. also here i made video for this issue please check & given below my c# code.

https://youtu.be/NxWh290JjZs

Please help me

 private Image bgImage;
 private Image joystickImage;
 public Vector3 InputDirection { set; get; }

 private void Start()
 {
     bgImage = GetComponent<Image>();
     joystickImage = transform.GetChild(0).GetComponent<Image>();
     InputDirection = Vector3.zero;
 }

 public virtual void OnDrag(PointerEventData Ped)
 {
     Vector2 pos = Vector2.zero;
     if (RectTransformUtility.ScreenPointToLocalPointInRectangle(bgImage.rectTransform, Ped.position, Ped.pressEventCamera, out pos))
     {
         pos.x = (pos.x / bgImage.rectTransform.sizeDelta.x);
         pos.y = (pos.y / bgImage.rectTransform.sizeDelta.y);

         float x = (bgImage.rectTransform.pivot.x == 1) ? pos.x * 2 + 1 : pos.x * 2 - 1;
         float y = (bgImage.rectTransform.pivot.y == 1) ? pos.y * 2 + 1 : pos.y * 2 - 1;

         InputDirection = new Vector3(x, 0, y);
         //InputDirection = new Vector3(pos.x * 1 + 1, 0, pos.y * 1 - 1);
         InputDirection = (InputDirection.magnitude > 1.0f) ? InputDirection.normalized : InputDirection;

         joystickImage.rectTransform.anchoredPosition = new Vector3(InputDirection.x * (bgImage.rectTransform.sizeDelta.x / 3), InputDirection.z * (bgImage.rectTransform.sizeDelta.y/3));
     }
 }

 public virtual void OnPointerDown(PointerEventData Ped)
 {
     OnDrag(Ped);
 }

 public virtual void OnPointerUp(PointerEventData Ped)
 {
     InputDirection = Vector3.zero;
     joystickImage.rectTransform.anchoredPosition = Vector3.zero;
 }

Question information

Language:
English Edit question
Status:
Expired
For:
Unity Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Launchpad Janitor (janitor) said :
#1

This question was expired because it remained in the 'Open' state without activity for the last 15 days.