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. Dismiss Notice

Trouble with multiple actions happening at the same time

Discussion in 'Scripting' started by Mercyonx, Jun 26, 2020.

  1. Mercyonx

    Mercyonx

    Joined:
    Jun 28, 2018
    Posts:
    7
    Hey, i'm a noobie programmer. I am making a pong game with a friend where the player is able to use different angles, charge the power of the paddle. I was programming the controls of the game, and i ran into trouble. When the player is charging the power of the paddle, the paddle is unable to change to the other angles. The player is supposed to be able to change to other angles during the charge up. I honestly don't know what to do.



    I made the controls into three scripts




    ////////////////////vvvvvvvvvvv movement script


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MathfCalmp_test : GameManager
    6. {
    7.     [SerializeField]
    8.     float moveSpeed=0;
    9.     Vector3 pos = new Vector3(25, 1, 0);
    10.     // Start is called before the first frame update
    11.  
    12.  
    13.  
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.  
    19.         Movement();
    20.            
    21.     }
    22.  
    23.  
    24.  
    25.     void Movement()
    26.     {
    27.               if (InputManager.LPaddle_Movement_Up())
    28.         {
    29.          
    30.             transform.Translate(0, 0, -1 * moveSpeed * Time.deltaTime);
    31.            transform.position = new Vector3(transform.position.x, transform.position.y, Mathf.Clamp(transform.position.z, -13f, 13f));
    32.         }
    33.  
    34.  
    35.         if (InputManager.LPaddle_Movement_Down())
    36.         {
    37.            
    38.             transform.Translate(0, 0, 1 * moveSpeed * Time.deltaTime);
    39.             transform.position = new Vector3(transform.position.x, transform.position.y, Mathf.Clamp(transform.position.z, -13f, 13f));
    40.         }
    41.  
    42.     }
    43.  
    44.    
    45. }
    46.  
    //////////////////////////////////////////////////////////////////vvvvvvv angle script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Enable_Disable : MonoBehaviour
    6. {
    7.     public GameObject NeturalPos;
    8.     public GameObject UpwardPos;
    9.     public GameObject DianolUp;
    10.     public GameObject StickPos;
    11.     public GameObject ChargerPos;
    12.     // Start is called before the first frame update
    13.     public bool isRotateKeyDown;
    14.  
    15.     //The varibles below are used for the charging manchinac
    16.  
    17.    
    18.     [SerializeField] private float slowDown = 0;
    19.  
    20.  
    21.     //this will be used to record the paddles original size
    22.  
    23.  
    24.     void Start()
    25.     {
    26.         isRotateKeyDown = false;
    27.         NeturalPos.SetActive(true);
    28.         UpwardPos.SetActive(false);
    29.         DianolUp.SetActive(false);
    30.         StickPos.SetActive(false);
    31.         ChargerPos.SetActive(false);
    32.        
    33.    
    34.      
    35.     }
    36.  
    37.     // Update is called once per frame
    38.     public void ResetNetrual()
    39.     {
    40.         NeturalPos.SetActive(true);
    41.         UpwardPos.SetActive(false);
    42.         DianolUp.SetActive(false);
    43.         StickPos.SetActive(false);
    44.         ChargerPos.SetActive(false);
    45.     }
    46.  
    47.  
    48.  
    49.  
    50.     // changes the scale of an object
    51.    
    52.  
    53.  
    54.  
    55.  
    56.  
    57.             void StickPaddle()
    58.     {
    59.            // StickMachinic(StickPos, playBall);
    60.             //Debug.Log("button is pressed");  
    61.     }
    62.  
    63.     void Rotate()
    64.     {
    65.        
    66.         if (InputManager.LPaddle_Angle_1())
    67.         {
    68.             ResetNetrual();
    69.             //ResetSize();
    70.             NeturalPos.SetActive(false);
    71.             DianolUp.SetActive(true);
    72.             isRotateKeyDown = true;
    73.  
    74.         }
    75.  
    76.         else if (InputManager.LPaddle_Angle_2())
    77.         {
    78.             ResetNetrual();
    79.             //ResetSize();
    80.             NeturalPos.SetActive(false);
    81.             UpwardPos.SetActive(true);
    82.             isRotateKeyDown = true;
    83.  
    84.         }
    85.  
    86.         else if (InputManager.LPaddle_Stick())//(Input.GetKey(KeyCode.E))
    87.         {
    88.             ResetNetrual();
    89.             //ResetSize();
    90.             NeturalPos.SetActive(false);
    91.             StickPos.SetActive(true);
    92.             isRotateKeyDown = true;
    93.         }
    94.      
    95.  
    96.         else if (isRotateKeyDown == true)
    97.         {
    98.             ResetNetrual();
    99.             isRotateKeyDown = false;
    100.         }
    101.     }
    102.     private void Update()
    103.     {
    104.         Rotate();
    105.         //StickPaddle();
    106.     }
    107. }
    108.  

    ////////////////////////////////////////////// vv charge up script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Charger : MonoBehaviour
    6. {
    7.  
    8.  
    9.     [SerializeField] private MathfCalmp_test movementAcessor;
    10.     [SerializeField] private Enable_Disable rotationAcessor;
    11.     [SerializeField] public float power = 1;
    12.  
    13.     public bool charingUp;
    14.     Vector3 originalSize;
    15.  
    16.  
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.         charingUp = false;
    21.         originalSize = transform.localScale;
    22.     }
    23.  
    24.  
    25.  
    26.  
    27.  
    28.     //functions
    29.  
    30.     void IncreaseSize()
    31.     {
    32.  
    33.         transform.localScale = new Vector3(Mathf.Clamp(transform.localScale.x, 1f, 20f), Mathf.Clamp(transform.localScale.y, 1f, 9f), Mathf.Clamp(transform.localScale.z, 1f, 6f));
    34.     }
    35.  
    36.  
    37.  
    38.     void ResetSize()
    39.     {
    40.         transform.localScale = originalSize;
    41.     }
    42.  
    43.  
    44.  
    45.  
    46.     private void ChargedHit()
    47.     {
    48.  
    49.         if(Input.GetKey(KeyCode.Q))
    50.         {
    51.             rotationAcessor.ResetNetrual();
    52.             //NeturalPos.SetActive(true);
    53.             rotationAcessor.ChargerPos.SetActive(true);
    54.             IncreaseSize();
    55.             charingUp = true;
    56.            // rotationAcessor.isRotateKeyDown = true;
    57.  
    58.         }
    59.         if (Input.GetKeyDown(KeyCode.Q))
    60.         {
    61.             ResetSize();
    62.             charingUp = false;
    63.         }
    64.     }
    65.  
    66.  
    67.  
    68.  
    69.  
    70.  
    71.     // Update is called once per frame
    72.     void Update()
    73.     {
    74.         ChargedHit();
    75.     }
    76. }
    77.  
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    Well, it seems you're calling rotationAcessor.ResetNetrual(); while the paddle is charging. Why are you doing that? Maybe you can stop doing that?
     
    PraetorBlue likes this.
  3. Mercyonx

    Mercyonx

    Joined:
    Jun 28, 2018
    Posts:
    7
    Thanks that helped, but now i am facing a problem when if I charge and use Angle 1, the paddle cannot move. the problem does not occur with other angles.
     
  4. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    There are probably several reasons, so you'll want to follow standard debugging approach of simplifying and verifying. If the "Angle 1" isn't working, go into your angle 1 code, and add some Debug.Log statements to display the current values for different variables, then run the game. Hopefully you'll see that some variable has a different value than you were expecting, and that will lead you to the cause of the issue.

    Later, when you get more comfortable with this approach, you can replace the Debug.Log statements with breakpoints, and debug using the Visual Studio debugger directly.
     
  5. Mercyonx

    Mercyonx

    Joined:
    Jun 28, 2018
    Posts:
    7
    what type of variables should i be looking for? I added Debug.Log to check if the program is registering inputs.

    the result is that the program will register the charge button and the button for angle one, but when i use the movement controls the program is not registering the input.

    However, if i use angle 2, both the charge button and angle 2 button are registered, and the program registers the movement controls

    is the problem that the controls is coded with if statements?
     
  6. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    It's possible you've got the inputs set up wrong in Unity's input manager. See if 'InputManager.LPaddle_Angle_1()' is ever true, in any code.
     
  7. Mercyonx

    Mercyonx

    Joined:
    Jun 28, 2018
    Posts:
    7
    what do you mean any code. i did just put input to tell me if the bool is true

    when i ran the project, if i press on the button to use angle 1, the value returned true. so when i did the charge button along with the angle 1 button, the bool returned true. however, again the project did not register the movement buttons while i held both charge and angle 1.
    yet i was able to move when i used charge and angle 2. so, the cause of the problem is lost on me





    Input Manager below



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public static class InputManager
    6. {
    7.  
    8.     //-------Movement
    9.  
    10.  
    11.     //----Left Paddle
    12.     public static bool LPaddle_Movement_Up()
    13.     {
    14.         return Input.GetButton("LPaddle_Up");
    15.        
    16.     }
    17.     public static bool LPaddle_Movement_Down()
    18.     {
    19.         return Input.GetButton("LPaddle_Down");
    20.     }
    21.  
    22.  
    23.  
    24.     //---------- Angles
    25.  
    26.     // keyboard key "a"
    27.     public static bool LPaddle_Angle_1()
    28.     {
    29.         return Input.GetButton("LPaddle_Angle1");
    30.     }
    31.  
    32.     // keyboard key "d"
    33.     public static bool LPaddle_Angle_2()
    34.     {
    35.         return Input.GetButton("LPaddle_Angle2");
    36.     }
    37.  
    38.  
    39.  
    40.  
    41.  
    42.  
    43.     //--------------other
    44.  
    45.  
    46.     //public static bool LPaddle_Charge()
    47.     //{
    48.       //  return Input.GetButton("LPaddle_Charge");
    49.     //}
    50.  
    51.     // keyboard key "d"
    52.     public static bool LPaddle_Stick()
    53.     {
    54.         return Input.GetButton("LPaddle_Stick");
    55.     }
    56.  
    57. }
    58.  





    below is enable_disable


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Enable_Disable : MonoBehaviour
    6. {
    7.  
    8.     public GameObject NeturalPos;
    9.     // forward slash
    10.     public GameObject UpwardPos;
    11.  
    12.     // \ the back slasj
    13.     public GameObject DianolUp;
    14.  
    15.     public GameObject StickPos;
    16.  
    17.     public GameObject ChargerPos;
    18.     // Start is called before the first frame update
    19.     public bool isRotateKeyDown;
    20.  
    21.     //The varibles below are used for the charging manchinac
    22.  
    23.    
    24.     [SerializeField] private float slowDown = 0;
    25.  
    26.  
    27.     //this will be used to record the paddles original size
    28.  
    29.  
    30.     void Start()
    31.     {
    32.         isRotateKeyDown = false;
    33.         NeturalPos.SetActive(true);
    34.         UpwardPos.SetActive(false);
    35.         DianolUp.SetActive(false);
    36.         StickPos.SetActive(false);
    37.         ChargerPos.SetActive(false);
    38.        
    39.    
    40.      
    41.     }
    42.  
    43.     // Update is called once per frame
    44.     public void ResetNetrual()
    45.     {
    46.         NeturalPos.SetActive(true);
    47.         UpwardPos.SetActive(false);
    48.         DianolUp.SetActive(false);
    49.         StickPos.SetActive(false);
    50.         ChargerPos.SetActive(false);
    51.     }
    52.  
    53.  
    54.  
    55.  
    56.     // changes the scale of an object
    57.    
    58.  
    59.  
    60.  
    61.  
    62.  
    63.             void StickPaddle()
    64.     {
    65.            // StickMachinic(StickPos, playBall);
    66.             //Debug.Log("button is pressed");  
    67.     }
    68.  
    69.     void Rotate()
    70.     {
    71.  
    72.         if (InputManager.LPaddle_Stick())//(Input.GetKey(KeyCode.E))
    73.         {
    74.             Debug.Log("works: stick");
    75.             Debug.Log("bool_stick:" + InputManager.LPaddle_Stick());
    76.             ResetNetrual();
    77.             //ResetSize();
    78.             NeturalPos.SetActive(false);
    79.             StickPos.SetActive(true);
    80.             isRotateKeyDown = true;
    81.         }
    82.        
    83.  
    84.         else if (InputManager.LPaddle_Angle_2())
    85.         {
    86.             Debug.Log("works: Angle 2");
    87.             Debug.Log("bool_Angle2:" + InputManager.LPaddle_Angle_2());
    88.             Debug.Log("bool_Angle 1:" + InputManager.LPaddle_Angle_1());
    89.             ResetNetrual();
    90.             //ResetSize();
    91.             NeturalPos.SetActive(false);
    92.             UpwardPos.SetActive(true);
    93.             isRotateKeyDown = true;
    94.  
    95.         }
    96.  
    97.         else if (InputManager.LPaddle_Angle_1())
    98.         {
    99.             Debug.Log("works: Angle 1");
    100.             Debug.Log("bool_Angle 1:" + InputManager.LPaddle_Angle_1());
    101.             ResetNetrual();
    102.             //ResetSize();
    103.             NeturalPos.SetActive(false);
    104.             DianolUp.SetActive(true);
    105.             isRotateKeyDown = true;
    106.  
    107.         }
    108.  
    109.  
    110.         else if (isRotateKeyDown == true)
    111.         {
    112.             ResetNetrual();
    113.             isRotateKeyDown = false;
    114.         }
    115.     }
    116.    
    117.  
    118.  
    119.  
    120.  
    121.  
    122.     private void Update()
    123.     {
    124.         Rotate();
    125.      
    126.      
    127.      
    128.        
    129.      
    130.      
    131.     }
    132. }
    133.  
     
  8. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    So you see the "works: Angle 1" messages in your console?

    Also, your Angle_1 and Angle_2 code being within 'else ifs', is it intended that if LPaddle_Stick is true, you should ignore Angle_1 and Angle_2?
     
  9. Mercyonx

    Mercyonx

    Joined:
    Jun 28, 2018
    Posts:
    7
    yeah i see "works: Angle 1" messages in my console.
    upload_2020-6-28_19-2-4.png

    as for the "else ifs," the "stick" action takes priority over the other actions.