Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Joystick UI

Discussion in 'Scripting' started by Shayke, Feb 13, 2018.

  1. Shayke

    Shayke

    Joined:
    Dec 8, 2017
    Posts:
    352
    Hi, i am trying to make a joystick to move my character.
    This is my code.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine.UI;
    4. using UnityEngine.EventSystems;
    5. using UnityEngine;
    6.  
    7. public class VirtualJoystick : MonoBehaviour , IDragHandler, IPointerDownHandler, IPointerUpHandler
    8. {
    9.  
    10.     private Image BgIMG,JoystickIMG;
    11.     private Vector3 InputVector;
    12.     private void Start()
    13.     {
    14.         BgIMG = GetComponent<Image>();
    15.         JoystickIMG = transform.GetChild(0).GetComponent<Image>();
    16.     }
    17.     public virtual void OnDrag(PointerEventData ped)
    18.     {
    19.         Vector2 pos;
    20.         if(RectTransformUtility.ScreenPointToLocalPointInRectangle(BgIMG.rectTransform, ped.position,ped.pressEventCamera,out pos))
    21.         {
    22.      
    23.             pos.x = (pos.x / BgIMG.rectTransform.sizeDelta.x);
    24.             pos.y = (pos.y / BgIMG.rectTransform.sizeDelta.y);
    25.  
    26.             InputVector = new Vector3(pos.x * 2 + 1, 0, pos.y * 2 - 1);
    27.             InputVector = (InputVector.magnitude > 1.0f) ? InputVector.normalized: InputVector;
    28.  
    29.             //Move joystick image
    30.             JoystickIMG.rectTransform.anchoredPosition = new Vector3(InputVector.x * (BgIMG.rectTransform.sizeDelta.x / 3), InputVector.z * (BgIMG.rectTransform.sizeDelta.y / 3));
    31.             Debug.Log(pos);
    32.         }
    33.     }
    34.     public virtual void OnPointerDown(PointerEventData ped)
    35.     {
    36.         OnDrag(ped);
    37.     }
    38.     public virtual void OnPointerUp(PointerEventData ped)
    39.     {
    40.         InputVector = Vector3.zero;
    41.         JoystickIMG.rectTransform.anchoredPosition = Vector3.zero;
    42.  
    43.     }
    44.     public float Horizontal()
    45.     {
    46.         if (InputVector.x != 0)
    47.         {
    48.             return InputVector.x;
    49.         }
    50.         else return Input.GetAxis("Horizontal");
    51.     }
    52.     public float Vertical()
    53.     {
    54.         if (InputVector.z != 0)
    55.         {
    56.             return InputVector.z;
    57.         }
    58.         else return Input.GetAxis("Vertical");
    59.     }
    60. }
    61.  
    And this is my code in my player
    Code (CSharp):
    1.     private Vector3 MoveInput()
    2.     {
    3.         Vector3 dir = Vector3.zero;
    4.         dir.x = Joystick.Horizontal();
    5.         dir.z = Joystick.Vertical();
    6.         if (dir.sqrMagnitude > 1)
    7.             dir.Normalize();
    8.  
    9.         return dir;
    10.  
    11.     }
    The joystick is moving and get back to his place when i release the mouse, but the character is not moving.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I'd add debugging to determine if dir.x and dir.y are getting non-zero values in MoveInput(). If so then I'd look at how you are using MoveInput()'s return value to move the character.
     
  3. Shayke

    Shayke

    Joined:
    Dec 8, 2017
    Posts:
    352
    it's zero, what should i change?
     
  4. Shayke

    Shayke

    Joined:
    Dec 8, 2017
    Posts:
    352
    Anyone?
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I'd continue adding debugging to figure out where that value is breaking down. Are you properly setting "Joystick" for example? Is OnPointerUp getting called, which resets InputVector to all 0's, before MoveInput is called? Put debugging everywhere and track what is getting called in what order, what values things have, and what might not be behaving as you expect.