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

How do you disable inputs?

Discussion in 'Scripting' started by Nuan07, Nov 6, 2021.

  1. Nuan07

    Nuan07

    Joined:
    Oct 24, 2021
    Posts:
    5
    I need to preface this question with an apology, I've only just started my coding/ unity journey 2 weeks ago and thought I'd start with a simple project. I want to make a 2D sliding block puzzle game similar to the one you can find in Zelda Twilight Princess. I have a few different movement scripts that I've written and the two functions that I just can't get to work for me are disabling player input while the blocks are moving and making a block immobile once they're at rest so other blocks can stack against them instead of pushing them along with them. I apologise if the question seems mind-numbingly stupid but I've been looking for over a week, trying to use a simple canMove bool, trying to use yield WaitForSeconds to measure if the blocks are in the same place or using an if statement if the velocity was equal to or greater than 0.1 to disallow my movement script in another script. I just can't get anything to work.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Movementver2: MonoBehaviour
    6. {
    7.     public Rigidbody2D rb;
    8.     private float moveSpeed = 10f;
    9.     public bool canMove = false;
    10.  
    11.     public const string UP = "up";
    12.     public const string DOWN = "down";
    13.     public const string LEFT = "left";
    14.     public const string RIGHT = "right";
    15.  
    16.     string moveDirection;
    17.  
    18.  
    19.     // Start is called before the first frame update
    20.     void Start()
    21.     {
    22.         rb = GetComponent<Rigidbody2D>();
    23.     }
    24.  
    25.     // Update is called once per frame
    26.     void Update()
    27.     {
    28.  
    29.         /*by using GetKeyDown, in this instance it only moves once and
    30.         not very far, requiring many key presses to move forward.*/
    31.       if(canMove == true)
    32.       {
    33.           if (Input.GetKeyDown(KeyCode.W))
    34.         {
    35.              moveDirection = UP;
    36.         }
    37.  
    38.         if (Input.GetKeyDown(KeyCode.S))
    39.         {
    40.             moveDirection = DOWN;
    41.         }
    42.  
    43.         if(Input.GetKeyDown(KeyCode.D))
    44.         {
    45.             moveDirection = RIGHT;
    46.         }
    47.  
    48.         if(Input.GetKeyDown(KeyCode.A))
    49.         {
    50.             moveDirection = LEFT;
    51.         }
    52.       }
    53.        
    54.        
    55. // if these are no else if statements, then this statement screws up the movement.
    56. // If you remove it then all the movement acts accordingly.
    57.        /* else
    58.         {
    59.             moveDirection = null;
    60.         }     */
    61.     }
    62.    
    63.  
    64. // I am able to get this code to work identically with only if statement
    65. // You can removes the else's and it still works - not sure why?
    66.     void FixedUpdate()
    67.     {
    68.        
    69.         if(moveDirection == UP)
    70.         {
    71.             rb.velocity = new Vector2(0, moveSpeed);
    72.         }
    73.  
    74.         if(moveDirection == DOWN)
    75.         {
    76.             rb.velocity = new Vector2(0, -moveSpeed);
    77.         }
    78.  
    79.          if(moveDirection == RIGHT)
    80.         {
    81.             rb.velocity = new Vector2(moveSpeed, 0);
    82.         }
    83.  
    84.          if(moveDirection == LEFT)
    85.         {
    86.             rb.velocity = new Vector2(-moveSpeed, 0);
    87.         }
    88.  
    89.  
    90.         /* if you remove this then the block will keep moving indefinately
    91.         with  a single key press instead of moving once even if it's colliding. */
    92.        /* else
    93.         {
    94.             rb.velocity = new Vector2(0, 0);
    95.         }*/
    96.  
    97.      
    98.  
    99.     }
    100. }
    I know the code is clunky, it's the only one that I wrote that would allow the blocks to slide continuously until they collided with a wall. Any help would prevent any further hair loss.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    I'm not familiar with the particular game's puzzles. Do they need actual physics? What are you actually moving, a player that exerts force on the blocks or the blocks themselves?
     
  3. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,397
    Use a bool (canMove or smt)
    if(canMove){ //Handle input here}
    else { velocity = 0;}

    Something like this works fine
     
  4. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    If you flip canMove to false you would disallow the input to change in Update forcing FixedUpdate to continue in moveDirection until that is reset and canMove set back to true. I also wouldn't use a string for moveDirection and instead use a enumeration so that when you change it you wont generate unreferenced memory.

    Create the enumeration that contains your directions.

    Code (CSharp):
    1. public enum MovementDirections
    2. {
    3.     None,
    4.     Up,
    5.     Down,
    6.     Left,
    7.     Right
    8. }

    You can declare moveDirection like this.

    Code (CSharp):
    1. MovementDirections moveDirection = MovementDirections.None;

    You can compare like this.

    Code (CSharp):
    1.  if(moveDirection == MovementDirections.Up)
    2.         {
    3.             rb.velocity = new Vector2(0, moveSpeed);
    4.         }