Search Unity

Virtual Joystick don't work when a new scene is loaded

Discussion in '2D' started by Renathohcc3, Mar 8, 2021.

  1. Renathohcc3

    Renathohcc3

    Joined:
    Mar 8, 2021
    Posts:
    1
    Hi guys, I'm a new dev, I'm making some courses and trying to make a mobile rpg2D. Everything was working fine, but, when I make the switching scenes script, I found a bug in the virtual joystick. When the player change a scene, the joystick just freeze and the player continue the last movement he had in the previous scene (Like shows in the video). I've tried so many ways to fix it, research in some foruns and sites, but nothing works. Someone have an idea of that I can do to fix it?

    Video showing the bug:


    Scripts:

    PlayerController:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     public Rigidbody2D theRB;
    8.     public float moveSpeed;
    9.     public FixedJoystick joystick;
    10.     public Animator myAnim;
    11.     private float hMove;
    12.     private float vMove;
    13.  
    14.     public static PlayerController instance;
    15.     public string areaTransitionName;
    16.  
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.         // Não duplicar o player ao trocar de cenas
    21.        if(instance == null){
    22.             instance = this;
    23.         }
    24.         else{
    25.             Destroy(instance);
    26.         }
    27.  
    28.         DontDestroyOnLoad(instance);
    29.     }
    30.  
    31.     // Update is called once per frame
    32.     void Update()
    33.     {
    34.         // Variaveis de Movimentacao
    35.         //hMove = Input.GetAxisRaw("Horizontal");
    36.         //vMove = Input.GetAxisRaw("Vertical");
    37.  
    38.         hMove = joystick.Horizontal;
    39.         vMove = joystick.Vertical;
    40.  
    41.         // Movimentacao do player
    42.         theRB.velocity = new Vector2 (hMove, vMove)* moveSpeed;
    43.  
    44.         // Animacao do Player
    45.         myAnim.SetFloat("moveX", theRB.velocity.x);
    46.         myAnim.SetFloat("moveY", theRB.velocity.y);
    47.  
    48.         if(hMove == 1 || hMove == -1 || vMove == 1 || vMove == -1)
    49.         {
    50.             myAnim.SetFloat("lastMoveX", hMove);
    51.             myAnim.SetFloat("lastMoveY", vMove);
    52.         }
    53.  
    54.     }
    55.  
    56. }
    AreaExit:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class AreaExit : MonoBehaviour {
    7.  
    8.     public string areaToLoad;
    9.     public string areaTransitionName;
    10.     public AreaEntrance theEntrance;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         theEntrance.nameTransition = areaTransitionName;
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.        
    22.     }
    23. // Função para trocar de cena ao entrar no trigger
    24.     private void OnTriggerEnter2D(Collider2D other)
    25.     {
    26.         if (other.tag == "Player")
    27.         {
    28.             SceneManager.LoadScene(areaToLoad);
    29.             PlayerController.instance.areaTransitionName = areaTransitionName;
    30.         }
    31.     }
    32. }
    AreaEntrance:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AreaEntrance : MonoBehaviour
    6. {
    7.     public string nameTransition;
    8.     // Start is called before the first frame update
    9.  
    10.     void Start()
    11.     {
    12.  
    13.         if(nameTransition == PlayerController.instance.areaTransitionName){
    14.             PlayerController.instance.transform.position = transform.position;
    15.            
    16.         }
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.  
    23.     }
    24. }
    **Some Comments in the code are in my native language (Portuguese), sorry about that. I wish you all can understant the codes.

    If someone wants to see more, I can send the github project link.
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,465
    A quick attempt to fix it is in Start of the PlayerController you can set the hMove and vMove to zero so every time the scene loads it resets those. Since you are using a singleton pattern, try putting hMove =0 and vMove = 0 under the else block where it does the Destroy, but above Destroy (just in case that deletes it before it can run).

    If that doesn't work, you could just get rid of the singleton pattern and have that script on your player so it gets re-instantiated with the player each scene.

    If neither of those work or you don't want to try the 2nd option, the issue appears to be taking your input from the previous scene and keeping it. So if you can find a way to reset it on each scene that should fix it.
     
    Renathohcc3 likes this.