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

Question Disabling touch input if possible or other ways around it.

Discussion in 'Scripting' started by PilumMuralis, Jun 10, 2020.

  1. PilumMuralis

    PilumMuralis

    Joined:
    Jun 6, 2020
    Posts:
    12
    The timeline i have in mind is as such.
    1. Game starts, player touches screen (input.touch), "player sprite" jumps off cliff - out of camera view, disable touch input (player cannot interact w game)
    2. "Player sprite" hits a specific position out of screen, trigger, "player sprite" flies back up into camera view, reaches intended position, enable touch input (player can interact w game, game starts).
    Is there a way I can carry out this course of action (enabling/disabling touch.input) via scripting?
    I have seen topics on people disabling inputs of mouse or keyboard, but have yet to come across one for touch screen input.
     
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    Not sure what you're asking is even possible and it seems pretty backwards.
    Usually you would disable things that read input.
    So whenever you enter a state that doesn't allow input, set via some boolean variable for example, stop acting on it.
     
  3. PilumMuralis

    PilumMuralis

    Joined:
    Jun 6, 2020
    Posts:
    12
    I do understand what you mean by backwards. I am new to coding and the whole logic that comes with it. I guess I will have to sort things out w bools if there isn't a particular line of C# code that can conveniently disable touch input.
     
  4. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    It's totally a valid question to ask. I think just having a bool somewhere to check for is the next best thing.
     
  5. PilumMuralis

    PilumMuralis

    Joined:
    Jun 6, 2020
    Posts:
    12
    I managed to do what I wanted with a bool! The other thing is that now I can't get the "Player sprite" to return to change direction after reaching a certain height. I basically can't get anything to trigger in the second IF statement. Would you mind glancing over to see what's the error?


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Controller : MonoBehaviour
    6. {
    7.     private Animator anim;
    8.     private Rigidbody2D rb2d;
    9.     private bool inputEnabled = true;
    10.     void Start()
    11.     {
    12.         anim = GetComponent<Animator>();
    13.         rb2d = GetComponent<Rigidbody2D>();
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.         foreach (Touch t in Input.touches) {
    19.             if (inputEnabled == true) {
    20.                 rb2d.velocity = new Vector2(3f, 5f);
    21.                 anim.SetBool("isOnGround", false);
    22.                 inputEnabled = false;
    23.                 }
    24.             }
    25.         if (transform.position.y <= -5.5f) {
    26.                 anim.SetBool("isFlying", true);
    27.                 transform.Translate(0 , 2*Time.deltaTime, 0);
    28.         }
    29.     }
    30. }
    31.