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): private Image bgImg; private Image joystickImg; public Vector3 InputDirection { set; get; } private void Start() { bgImg = GetComponent<Image>(); joystickImg = transform.GetChild(0).GetComponent<Image>(); InputDirection = Vector3.zero; } public virtual void OnDrag (PointerEventData ped) { Vector2 pos = Vector2.zero; if (RectTransformUtility.ScreenPointToLocalPointInRectangle (bgImg.rectTransform, ped.position, ped.pressEventCamera, out pos)) { pos.x = (pos.x / bgImg.rectTransform.sizeDelta.x); float x = (bgImg.rectTransform.pivot.x == 1) ? pos.x * 2 + 1 : pos.x * 2 - 1; InputDirection = new Vector3(x, 0, 0); InputDirection = (InputDirection.magnitude > 1) ? InputDirection.normalized : InputDirection; joystickImg.rectTransform.anchoredPosition = new Vector3(InputDirection.x * (bgImg.rectTransform.sizeDelta.x / 3), 0); } } public virtual void OnPointerDown(PointerEventData ped) { OnDrag (ped); } public virtual void OnPointerUp(PointerEventData ped) { InputDirection = Vector3.zero; joystickImg.rectTransform.anchoredPosition = Vector3.zero; } And this is script for player: Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class PlayerControl : MonoBehaviour { float speed; public int manaNum; public int maxMana; public float accelerate; public float jumpForce; public bool grounded; Rigidbody2D rb; public VirtualJoystick moveJoystick; GameObject mana; private Touch touch; void Start () { rb = GetComponent<Rigidbody2D>(); mana = GameObject.Find("ManaText"); manaNum = maxMana; } void Update() { Mana(); if (grounded) if (moveJoystick.InputDirection != Vector3.zero) { speed = moveJoystick.InputDirection.x; } else { speed = 0; } rb.velocity = new Vector2(speed * accelerate, rb.velocity.y); Debug.Log(speed); if (speed >= 0) { transform.rotation = Quaternion.Euler(new Vector3(0, 0, 0)); } else { transform.rotation = Quaternion.Euler(new Vector3(0, 180, 0)); } } public void Jump() { if (grounded) rb.velocity = new Vector2(0, jumpForce); } void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.tag == "Ground") { grounded = true; } if (collision.gameObject.tag == "Star") { if (manaNum < maxMana) { manaNum += 1; Destroy(collision.gameObject); } } } void OnCollisionExit2D(Collision2D collision) { if (collision.gameObject.tag == "Ground") { grounded = false; } } void Mana() { mana.GetComponent<Text>().text = "MANA: " + manaNum.ToString(); float relativeManaBar = (float)manaNum / (float)maxMana; GameObject.Find("ManaBar").transform.localScale = new Vector3(relativeManaBar, transform.localScale.y, transform.localScale.z); } 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.
Ok I find where is problem, i just changed: Code (CSharp): void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.tag == "Ground") { grounded = true; } if (collision.gameObject.tag == "Star") { if (manaNum < maxMana) { manaNum += 1; Destroy(collision.gameObject); } } } void OnCollisionExit2D(Collision2D collision) { if (collision.gameObject.tag == "Ground") { grounded = false; } } for this: Code (CSharp): void OnCollisionStay2D(Collision2D collision) { if (collision.gameObject.tag == "Ground") { grounded = true; } } void OnTriggerEnter2D(Collider2D collision) { if (collision.gameObject.tag == "Star") { if (manaNum < maxMana) { manaNum += 1; Destroy(collision.gameObject); } } } void OnCollisionExit2D(Collision2D collision) { if (collision.gameObject.tag == "Ground") { grounded = false; } }