Search Unity

Freezing move with joystick

Discussion in 'Android' started by SVKsuli, Mar 25, 2017.

  1. SVKsuli

    SVKsuli

    Joined:
    Mar 23, 2014
    Posts:
    63
    Hello, im new in andoid and i try to create some basic runner game, I have basic joystick for control moving and button for jump, problem is when i force player in to the wall and then turn to oposite side my control freez, player slowly move and i cant stop it, i must restart game to fix it. It happend sometimes in different situation.
    (all controls just freez and cant do anything).

    This is my script for control joystick:
    Code (CSharp):
    1. private Image bgImg;
    2.     private Image joystickImg;
    3.  
    4.     public Vector3 InputDirection { set; get; }
    5.  
    6.     private void Start()
    7.     {
    8.         bgImg = GetComponent<Image>();
    9.         joystickImg = transform.GetChild(0).GetComponent<Image>();
    10.         InputDirection = Vector3.zero;
    11.     }
    12.  
    13.     public virtual void OnDrag (PointerEventData ped)
    14.     {
    15.         Vector2 pos = Vector2.zero;
    16.         if (RectTransformUtility.ScreenPointToLocalPointInRectangle
    17.             (bgImg.rectTransform, ped.position, ped.pressEventCamera, out pos))
    18.         {
    19.             pos.x = (pos.x / bgImg.rectTransform.sizeDelta.x);
    20.  
    21.             float x = (bgImg.rectTransform.pivot.x == 1) ? pos.x * 2 + 1 : pos.x * 2 - 1;
    22.  
    23.             InputDirection = new Vector3(x, 0, 0);
    24.             InputDirection = (InputDirection.magnitude > 1) ? InputDirection.normalized : InputDirection;
    25.  
    26.             joystickImg.rectTransform.anchoredPosition =
    27.                 new Vector3(InputDirection.x * (bgImg.rectTransform.sizeDelta.x / 3), 0);
    28.         }
    29.     }
    30.  
    31.     public virtual void OnPointerDown(PointerEventData ped)
    32.     {
    33.         OnDrag (ped);
    34.     }
    35.  
    36.     public virtual void OnPointerUp(PointerEventData ped)
    37.     {
    38.         InputDirection = Vector3.zero;
    39.         joystickImg.rectTransform.anchoredPosition = Vector3.zero;
    40.     }
    And this is script for player:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class PlayerControl : MonoBehaviour {
    7.  
    8.     float speed;
    9.     public int manaNum;
    10.     public int maxMana;
    11.     public float accelerate;
    12.     public float jumpForce;
    13.     public bool grounded;
    14.     Rigidbody2D rb;
    15.     public VirtualJoystick moveJoystick;
    16.     GameObject mana;
    17.  
    18.     private Touch touch;
    19.  
    20.     void Start () {
    21.         rb = GetComponent<Rigidbody2D>();
    22.         mana = GameObject.Find("ManaText");
    23.         manaNum = maxMana;
    24.        
    25.     }
    26.  
    27.     void Update()
    28.     {
    29.         Mana();
    30.  
    31.         if (grounded)
    32.             if (moveJoystick.InputDirection != Vector3.zero)
    33.             {
    34.                 speed = moveJoystick.InputDirection.x;
    35.             }
    36.             else
    37.             {
    38.                 speed = 0;
    39.             }
    40.  
    41.         rb.velocity = new Vector2(speed * accelerate, rb.velocity.y);
    42.         Debug.Log(speed);
    43.  
    44.         if (speed >= 0)
    45.         {
    46.             transform.rotation = Quaternion.Euler(new Vector3(0, 0, 0));
    47.         }
    48.         else
    49.         {
    50.             transform.rotation = Quaternion.Euler(new Vector3(0, 180, 0));
    51.         }
    52.     }
    53.  
    54.     public void Jump()
    55.     {
    56.         if (grounded)
    57.             rb.velocity = new Vector2(0, jumpForce);
    58.     }
    59.  
    60.     void OnCollisionEnter2D(Collision2D collision)
    61.     {
    62.         if (collision.gameObject.tag == "Ground")
    63.         {
    64.             grounded = true;
    65.         }
    66.  
    67.         if (collision.gameObject.tag == "Star")
    68.         {
    69.             if (manaNum < maxMana)
    70.             {
    71.                 manaNum += 1;
    72.                 Destroy(collision.gameObject);
    73.             }
    74.         }
    75.     }
    76.  
    77.     void OnCollisionExit2D(Collision2D collision)
    78.     {
    79.         if (collision.gameObject.tag == "Ground")
    80.         {
    81.             grounded = false;
    82.         }
    83.     }
    84.    
    85.     void Mana()
    86.     {
    87.         mana.GetComponent<Text>().text = "MANA: " + manaNum.ToString();
    88.         float relativeManaBar = (float)manaNum / (float)maxMana;
    89.         GameObject.Find("ManaBar").transform.localScale = new Vector3(relativeManaBar, transform.localScale.y, transform.localScale.z);
    90.     }
    When i try to use Debug.Log() for speed information, then i can see, when it freez then speed is not 0 but 0.12...
    I think it look like problem come from:
    if (moveJoystick.InputDirection != Vector3.zero)
    {
    speed = moveJoystick.InputDirection.x;
    }
    But i dont know if i have true and what to do with it.

    Thx for help.
     
  2. SVKsuli

    SVKsuli

    Joined:
    Mar 23, 2014
    Posts:
    63
    Ok I find where is problem, i just changed:
    Code (CSharp):
    1. void OnCollisionEnter2D(Collision2D collision)
    2.     {
    3.         if (collision.gameObject.tag == "Ground")
    4.         {
    5.             grounded = true;
    6.         }
    7.         if (collision.gameObject.tag == "Star")
    8.         {
    9.             if (manaNum < maxMana)
    10.             {
    11.                 manaNum += 1;
    12.                 Destroy(collision.gameObject);
    13.             }
    14.         }
    15.     }
    16.     void OnCollisionExit2D(Collision2D collision)
    17.     {
    18.         if (collision.gameObject.tag == "Ground")
    19.         {
    20.             grounded = false;
    21.         }
    22.     }
    for this:
    Code (CSharp):
    1. void OnCollisionStay2D(Collision2D collision)
    2.     {
    3.         if (collision.gameObject.tag == "Ground")
    4.         {
    5.             grounded = true;
    6.         }
    7.     }
    8.     void OnTriggerEnter2D(Collider2D collision)
    9.     {
    10.         if (collision.gameObject.tag == "Star")
    11.         {
    12.             if (manaNum < maxMana)
    13.             {
    14.                 manaNum += 1;
    15.                 Destroy(collision.gameObject);
    16.             }
    17.         }
    18.     }
    19.     void OnCollisionExit2D(Collision2D collision)
    20.     {
    21.         if (collision.gameObject.tag == "Ground")
    22.         {
    23.             grounded = false;
    24.         }
    25.     }