Search Unity

Stopping player movement

Discussion in 'Scripting' started by DustyShinigami, Jul 22, 2019.

  1. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    529
    Okay, so the first instance of this, I managed to fix. Basically, if I pressed the interact button to pick up something, the interact animation would play and the item would disappear. If I pressed interact but then moved left/right at the same time, the character's animation would be interrupted, and instead of walking, he would float along the floor until stopping. As I said, I managed to fix the first case of this by doing everything in a coroutine and stopping the player's movement for a second or two.
    However, if I talk to an NPC first, and then attempt to pick up the item and move left/right at the same time, the issue happens again. I'm not sure why as the coroutine is supposed to stop the player's movement once the item is interacted with. If the NPC has been interacted with AND the pickup has been collected, a spotlight activates indicating the player can leave the scene. The coroutine for picking up the item is located in my PlayerController script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class PlayerController : MonoBehaviour
    7. {
    8.     public Animator anim;
    9.     public float moveSpeed;
    10.     public float jumpForce;
    11.     public bool jumped;
    12.     public bool allowInteract = false;
    13.     public float gravityScale;
    14.     public float knockBackForce;
    15.     public float knockBackTime;
    16.     //public Material textureChange;
    17.     //public Material textureDefault;
    18.     public bool allowCombat;
    19.     public bool allowJump;
    20.     public static bool canMove;
    21.     public ChestTrigger chest
    22.     {
    23.         get
    24.         {
    25.             if (_chest != null)
    26.                 return _chest;
    27.  
    28.             _chest = GameObject.FindWithTag("Chest Trigger")?.GetComponent<ChestTrigger>();
    29.             return _chest;
    30.         }
    31.     }
    32.     //public SkinnedMeshRenderer playerRenderer;
    33.     //public GameObject playerRendererRef;
    34.     //public static PlayerController instance;
    35.  
    36.     private ChestTrigger _chest = null;
    37.     private Vector2 moveDirection;
    38.     private Vector2 moveHorizontal;
    39.     private float knockBackCounter;
    40.     private CharacterController controller;
    41.     private Quaternion targetRot;
    42.     private bool headingLeft = false;
    43.     private Pickup pickupWeapon;
    44.  
    45.     void Awake()
    46.     {
    47.         controller = GetComponent<CharacterController>();
    48.         anim = GetComponent<Animator>();
    49.         //playerRenderer = playerRendererRef.GetComponent<SkinnedMeshRenderer>();
    50.         /*if(instance != null)
    51.         {
    52.             Destroy(gameObject);
    53.         }
    54.         else
    55.         {
    56.             DontDestroyOnLoad(gameObject);
    57.             instance = this;
    58.         }*/
    59.     }
    60.  
    61.     void Start()
    62.     {
    63.         Cursor.visible = false;
    64.         pickupWeapon = FindObjectOfType<Pickup>();
    65.         canMove = true;
    66.         targetRot = transform.rotation;
    67.         if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("start_area"))
    68.         {
    69.             allowCombat = false;
    70.             allowJump = true;
    71.         }
    72.         if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("hut_interior"))
    73.         {
    74.             allowCombat = false;
    75.             allowJump = false;
    76.         }
    77.         if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("start_area 2"))
    78.         {
    79.             allowCombat = false;
    80.             allowJump = true;
    81.         }
    82.         if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("level 1, room 1"))
    83.         {
    84.             allowCombat = true;
    85.             allowJump = true;
    86.         }
    87.         if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("level 1, room 2"))
    88.         {
    89.             allowCombat = true;
    90.             allowJump = true;
    91.         }
    92.     }
    93.  
    94.     void Update()
    95.     {
    96.         //To avoid any control or animation issues, each code block must be within this code block
    97.         if (knockBackCounter <= 0 && canMove)
    98.         {
    99.             moveHorizontal.x = Input.GetAxis("Horizontal");
    100.             //moveVertical.y = Input.GetAxis("Vertical");
    101.             moveDirection = new Vector2(moveHorizontal.x * moveSpeed, moveDirection.y);
    102.             controller.Move(moveDirection * Time.deltaTime);
    103.  
    104.             //Adds character rotation when changing direction horizontally
    105.             if ((moveHorizontal.x < 0f && !headingLeft) || (moveHorizontal.x > 0f && headingLeft))
    106.             {
    107.                 if (moveHorizontal.x < 0f) targetRot = Quaternion.Euler(0, 270, 0);
    108.                 if (moveHorizontal.x > 0f) targetRot = Quaternion.Euler(0, 90, 0);
    109.                 headingLeft = !headingLeft;
    110.             }
    111.             transform.rotation = Quaternion.Lerp(transform.rotation, targetRot, Time.deltaTime * 20f);
    112.  
    113.             //Adds character rotation when changing direction vertically
    114.             /*if(moveVertical.y < 0f && lookingUp || (moveVertical.y > 0f && !lookingUp))
    115.             {
    116.                 if (moveVertical.y > 0f) targetrot = Quaternion.Euler(0, 0, 0);
    117.                 if (moveVertical.y < 0f) targetrot = Quaternion.Euler(0, 180, 0);
    118.                 lookingUp = !lookingUp;
    119.             }*/
    120.  
    121.             if (SceneManagement.insideHut && canMove)
    122.             {
    123.                 float moveHorizontalSnap = Input.GetAxis("Horizontal");
    124.                 float moveVerticalSnap = Input.GetAxis("Vertical");
    125.  
    126.                 //Adds character rotation when changing direction horizontally, but snaps instead of fully rotating
    127.                 if (moveHorizontalSnap > 0)
    128.                 {
    129.                     transform.eulerAngles = new Vector2(0, 90);
    130.                 }
    131.                 else if (moveHorizontalSnap < 0)
    132.                 {
    133.                     transform.eulerAngles = new Vector2(0, -90);
    134.                 }
    135.  
    136.                 //To possibly prevent diagonal movement with some control setups, try adding 'else if'
    137.                 //Adds character rotation when changing direction vertically, but snaps instead of fully rotating
    138.                 else if (moveVerticalSnap > 0)
    139.                 {
    140.                     transform.eulerAngles = new Vector2(0, 0);
    141.                 }
    142.  
    143.                 //Use this to make the character face towards the camera.
    144.                 /*else if (moveVertical < 0)
    145.                 {
    146.                     transform.eulerAngles = new Vector3(0, 180);
    147.                 }*/
    148.             }
    149.             if (controller.isGrounded)
    150.             {
    151.                 if (allowJump)
    152.                 {
    153.                     moveDirection.y = -1f;
    154.                     //GetKeyDown will require the player to press the button each time they want to jump. GetKey will allow the player to spam the jump button if they keep pressing it down.
    155.                     if (Input.GetKeyDown(KeyCode.KeypadPlus))
    156.                     {
    157.                         moveDirection.y = jumpForce;
    158.                         jumped = true;
    159.                     }
    160.                     else if (!Input.GetKeyDown(KeyCode.KeypadPlus))
    161.                     {
    162.                         jumped = false;
    163.                     }
    164.                     if (SceneManagement.xbox360Controller == 1)
    165.                     {
    166.                         if (Input.GetKeyDown("joystick button 0"))
    167.                         {
    168.                             moveDirection.y = jumpForce;
    169.                             jumped = true;
    170.                         }
    171.                         else if (!Input.GetKeyDown("joystick button 0"))
    172.                         {
    173.                             jumped = false;
    174.                         }
    175.                     }
    176.                     else if (SceneManagement.ps4Controller == 1)
    177.                     {
    178.                         if (Input.GetKeyDown("joystick button 1"))
    179.                         {
    180.                             moveDirection.y = jumpForce;
    181.                             jumped = true;
    182.                         }
    183.                         else if (!Input.GetKeyDown("joystick button 1"))
    184.                         {
    185.                             jumped = false;
    186.                         }
    187.                     }
    188.                 }
    189.             }
    190.             if (allowCombat)
    191.             {
    192.                 if (Input.GetKeyDown(KeyCode.Space))
    193.                 {
    194.                     anim.SetTrigger("Attack");
    195.                     //GameObject projectileObject = Instantiate(projectilePrefab);
    196.                     //projectilePrefab.transform.position = spawnPoint.transform.position + spawnPoint.transform.forward;
    197.                 }
    198.                 else if (SceneManagement.xbox360Controller == 1)
    199.                 {
    200.                     if (Input.GetKeyDown("joystick button 1"))
    201.                     {
    202.                         anim.SetTrigger("Attack");
    203.                     }
    204.                 }
    205.                 else if (SceneManagement.ps4Controller == 1)
    206.                 {
    207.                     if (Input.GetKeyDown("joystick button 2"))
    208.                     {
    209.                         anim.SetTrigger("Attack");
    210.                     }
    211.                 }
    212.             }
    213.             if (allowInteract)
    214.             {
    215.                 if (SceneManagement.xbox360Controller == 1)
    216.                 {
    217.                     if (Input.GetKeyDown("joystick button 2"))
    218.                     {
    219.                         StartCoroutine("Pickup");
    220.                     }
    221.                 }
    222.                 else if (SceneManagement.ps4Controller == 1)
    223.                 {
    224.                     if (Input.GetKeyDown("joystick button 0"))
    225.                     {
    226.                         StartCoroutine("Pickup");
    227.                     }
    228.                 }
    229.                 else
    230.                 {
    231.                     if (Input.GetKeyDown(KeyCode.Return))
    232.                     {
    233.                         StartCoroutine("Pickup");
    234.                     }
    235.                 }
    236.             }
    237.             if (ChestTrigger.allowOpen)
    238.             {
    239.                 if (Input.GetKeyDown(KeyCode.Return))
    240.                 {
    241.                     StartCoroutine("Open");
    242.                 }
    243.                 else if (Input.GetKeyDown(KeyCode.Return) && !ChestTrigger.allowOpen)
    244.                 {
    245.                     anim.SetBool("Interact", false);
    246.                 }
    247.                 else if (SceneManagement.xbox360Controller == 1)
    248.                 {
    249.                     if (Input.GetKeyDown("joystick button 2"))
    250.                     {
    251.                         StartCoroutine("Open");
    252.                     }
    253.                     else if (Input.GetKeyDown("joystick button 2") && !ChestTrigger.allowOpen)
    254.                     {
    255.                         anim.SetBool("Interact", false);
    256.                     }
    257.                 }
    258.                 else if (SceneManagement.ps4Controller == 1)
    259.                 {
    260.                     if (Input.GetKeyDown("joystick button 0"))
    261.                     {
    262.                         StartCoroutine("Open");
    263.                     }
    264.                     else if (Input.GetKeyDown("joystick button 0") && !ChestTrigger.allowOpen)
    265.                     {
    266.                         anim.SetBool("Interact", false);
    267.                     }
    268.                 }
    269.             }
    270.         }
    271.         else
    272.         {
    273.             knockBackCounter -= Time.deltaTime;
    274.         }
    275.         moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale * Time.deltaTime);
    276.  
    277.         anim.SetBool("isGrounded", controller.isGrounded);
    278.         //If the character can't move, then the Speed is set to 0. Otherwise it'll use the horizontal input value.
    279.         anim.SetFloat("Speed",
    280.             !canMove
    281.             ? 0f
    282.             : Mathf.Abs(Input.GetAxis("Horizontal")));
    283.  
    284.         //anim.SetFloat("Speed", Mathf.Abs(Input.GetAxis("Horizontal")));
    285.     }
    286.  
    287.     public IEnumerator Pickup()
    288.     {
    289.         canMove = false;
    290.         anim.SetBool("Interact", controller.isGrounded);
    291.         pickupWeapon.ObjectActivation();
    292.         yield return new WaitForSeconds(1f);
    293.         allowInteract = false;
    294.         canMove = true;
    295.     }
    296.  
    297.     public IEnumerator Open()
    298.     {
    299.         canMove = false;
    300.         anim.SetBool("Interact", controller.isGrounded);
    301.         chest.ChestOpen();
    302.         yield return new WaitForSeconds(1.5f);
    303.         canMove = true;
    304.     }
    305.  
    306.     public void Knockback(Vector3 direction)
    307.     {
    308.         knockBackCounter = knockBackTime;
    309.  
    310.         moveDirection = direction * knockBackForce;
    311.         moveDirection.y = knockBackForce;
    312.     }
    313. }
    And here are my Pickup, DialogueManager, and SceneManagement scripts:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Pickup : MonoBehaviour
    6. {
    7.     public GameObject[] buttonPrompts;
    8.     public GameObject rayGun;
    9.     public GameObject pickupLight;
    10.     public static bool objectsDisabled;
    11.     public GameObject thePlayer;
    12.  
    13.     private PlayerController thePlayerController;
    14.  
    15.     void Awake()
    16.     {
    17.         thePlayerController = thePlayer.GetComponent<PlayerController>();
    18.         objectsDisabled = false;
    19.     }
    20.  
    21.     public void OnTriggerEnter(Collider other)
    22.     {
    23.         if (other.gameObject.CompareTag("Player"))
    24.         {
    25.             thePlayerController.allowInteract = true;
    26.             ControllerDetection();
    27.             if (SceneManagement.ps4Controller == 1)
    28.             {
    29.                 PS4Prompts();
    30.             }
    31.             else if (SceneManagement.xbox360Controller == 1)
    32.             {
    33.                 Xbox360Prompts();
    34.             }
    35.             else
    36.             {
    37.                 PCPrompts();
    38.             }
    39.         }
    40.     }
    41.  
    42.     public void OnTriggerExit(Collider other)
    43.     {
    44.         thePlayerController.allowInteract = false;
    45.     }
    46.  
    47.     public void ObjectActivation()
    48.     {
    49.         rayGun.SetActive(false);
    50.         pickupLight.SetActive(false);
    51.         objectsDisabled = true;
    52.         GetComponent<Collider>().enabled = false;
    53.     }
    54.  
    55.     public void Hide()
    56.     {
    57.         buttonPrompts[0].SetActive(false);
    58.         buttonPrompts[1].SetActive(false);
    59.         buttonPrompts[2].SetActive(false);
    60.     }
    61.  
    62.     public void Xbox360Prompts()
    63.     {
    64.         buttonPrompts[1].SetActive(true);
    65.         Invoke("Hide", 3f);
    66.         if (objectsDisabled)
    67.         {
    68.             buttonPrompts[1].SetActive(false);
    69.         }
    70.     }
    71.  
    72.     public void PS4Prompts()
    73.     {
    74.         buttonPrompts[2].SetActive(true);
    75.         Invoke("Hide", 3f);
    76.         if (objectsDisabled)
    77.         {
    78.             buttonPrompts[2].SetActive(false);
    79.         }
    80.     }
    81.  
    82.     public void PCPrompts()
    83.     {
    84.         buttonPrompts[0].SetActive(true);
    85.         Invoke("Hide", 3f);
    86.         if (objectsDisabled)
    87.         {
    88.             buttonPrompts[0].SetActive(false);
    89.         }
    90.     }
    91.  
    92.     public void ControllerDetection()
    93.     {
    94.         string[] names = Input.GetJoystickNames();
    95.         for (int x = 0; x < names.Length; x++)
    96.         {
    97.             //print(names[x].Length);
    98.             if (names[x].Length == 19)
    99.             {
    100.                 //print("PS4 CONTROLLER IS CONNECTED");
    101.                 SceneManagement.ps4Controller = 1;
    102.                 SceneManagement.xbox360Controller = 0;
    103.                 if (SceneManagement.ps4Controller == 1)
    104.                 {
    105.                     //Debug.Log("PS4 controller detected");
    106.                 }
    107.             }
    108.             else if (names[x].Length == 33)
    109.             {
    110.                 //print("XBOX 360 CONTROLLER IS CONNECTED");
    111.                 SceneManagement.ps4Controller = 0;
    112.                 SceneManagement.xbox360Controller = 1;
    113.                 if (SceneManagement.xbox360Controller == 1)
    114.                 {
    115.                     //Debug.Log("Xbox 360 controller detected");
    116.                 }
    117.             }
    118.             else
    119.             {
    120.                 SceneManagement.ps4Controller = 0;
    121.                 SceneManagement.xbox360Controller = 0;
    122.             }
    123.  
    124.             if (SceneManagement.xbox360Controller == 0 && SceneManagement.ps4Controller == 0)
    125.             {
    126.                 //Debug.Log("No controllers detected");
    127.             }
    128.         }
    129.     }
    130. }
    131.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6.  
    7. public class DialogueManager : MonoBehaviour
    8. {
    9.     public GameObject dialogueBox;
    10.     public TextMeshProUGUI nameDisplay;
    11.     public GameObject[] buttonPrompts;
    12.     public TextMeshProUGUI textDisplay;
    13.     public string[] sentences;
    14.     public string[] characterName;
    15.     public float typingSpeed;
    16.  
    17.     private int xbox360Controller = 0;
    18.     private int pS4Controller = 0;
    19.     private int index;
    20.     private BoxCollider dialogueTrigger;
    21.     private int currentLine;
    22.     private bool interactPressed;
    23.     private bool continuePressed;
    24.     private bool endDialogue;
    25.     private bool continueAllowed;
    26.     public bool characterVicinity;
    27.     private bool dialogueActive;
    28.     private NPC theNPC;
    29.  
    30.     void Start()
    31.     {
    32.         dialogueTrigger = GetComponent<BoxCollider>();
    33.         theNPC = FindObjectOfType<NPC>();
    34.     }
    35.  
    36.     IEnumerator Type()
    37.     {
    38.         foreach (char letter in sentences[index].ToCharArray())
    39.         {
    40.             textDisplay.text += letter;
    41.             yield return new WaitForSeconds(typingSpeed);
    42.         }
    43.     }
    44.  
    45.     public void NextSentence()
    46.     {
    47.         buttonPrompts[3].SetActive(false);
    48.         buttonPrompts[4].SetActive(false);
    49.         buttonPrompts[5].SetActive(false);
    50.         //If Index has less than the number of elements in the 'sentences' array by -1
    51.         if (index < sentences.Length - 1)
    52.         {
    53.             index++;
    54.             //Resets textDisplay so sentences don't stack
    55.             textDisplay.text = "";
    56.             StartCoroutine(Type());
    57.         }
    58.         else
    59.         {
    60.             textDisplay.text = "";
    61.         }
    62.     }
    63.  
    64.     public void OnTriggerStay(Collider other)
    65.     {
    66.         if (other.gameObject.name == "Player")
    67.         {
    68.             characterVicinity = true;
    69.             ControllerDetection();
    70.             if (pS4Controller == 1)
    71.             {
    72.                 PS4Prompts();
    73.             }
    74.             else if (xbox360Controller == 1)
    75.             {
    76.                 Xbox360Prompts();
    77.             }
    78.             else
    79.             {
    80.                 PCPrompts();
    81.             }
    82.         }
    83.     }
    84.  
    85.     public void OnTriggerExit(Collider other)
    86.     {
    87.         characterVicinity = false;
    88.     }
    89.  
    90.     public void Update()
    91.     {
    92.         if (characterVicinity)
    93.         {
    94.             dialogueActive = false;
    95.             //Set conditions to prevent button spamming. The Return key will only work if the dialogue box isn't active. If it is, it won't do anything.
    96.             if (Input.GetKeyUp(KeyCode.Return) && !dialogueBox.activeSelf)
    97.             {
    98.                 PlayerController.canMove = false;
    99.                 interactPressed = true;
    100.                 if (interactPressed)
    101.                 {
    102.                     continueAllowed = false;
    103.                     nameDisplay.enabled = true;
    104.                     dialogueBox.SetActive(true);
    105.                     StartCoroutine("Type");
    106.                 }
    107.             }
    108.             else if (SceneManagement.xbox360Controller == 1)
    109.             {
    110.                 if (Input.GetKeyUp("joystick button 2") && !dialogueBox.activeSelf)
    111.                 {
    112.                     PlayerController.canMove = false;
    113.                     interactPressed = true;
    114.                     if (interactPressed)
    115.                     {
    116.                         continueAllowed = false;
    117.                         nameDisplay.enabled = true;
    118.                         dialogueBox.SetActive(true);
    119.                         StartCoroutine("Type");
    120.                     }
    121.                 }
    122.             }
    123.             else if (SceneManagement.ps4Controller == 1)
    124.             {
    125.                 if (Input.GetKeyUp("joystick button 0") && !dialogueBox.activeSelf)
    126.                 {
    127.                     PlayerController.canMove = false;
    128.                     interactPressed = true;
    129.                     if (interactPressed)
    130.                     {
    131.                         continueAllowed = false;
    132.                         nameDisplay.enabled = true;
    133.                         dialogueBox.SetActive(true);
    134.                         StartCoroutine("Type");
    135.                     }
    136.                 }
    137.             }
    138.         }
    139.         if (nameDisplay.enabled && dialogueBox.activeSelf)
    140.         {
    141.             dialogueActive = true;
    142.             if (SceneManagement.xbox360Controller == 1)
    143.             {
    144.                 if (buttonPrompts[4].activeSelf && Input.GetKeyUp("joystick button 2"))
    145.                 {
    146.                     currentLine++;
    147.                     if (currentLine >= sentences.Length)
    148.                     {
    149.                         nameDisplay.enabled = false;
    150.                         dialogueBox.SetActive(false);
    151.                         dialogueActive = false;
    152.                         endDialogue = true;
    153.                         characterVicinity = false;
    154.                         dialogueTrigger.enabled = false;
    155.                         theNPC.MoveRight();
    156.                     }
    157.                 }
    158.             }
    159.             else if (SceneManagement.ps4Controller == 1)
    160.             {
    161.                 if (buttonPrompts[5].activeSelf && Input.GetKeyUp("joystick button 0"))
    162.                 {
    163.                     currentLine++;
    164.                     if (currentLine >= sentences.Length)
    165.                     {
    166.                         nameDisplay.enabled = false;
    167.                         dialogueBox.SetActive(false);
    168.                         dialogueActive = false;
    169.                         endDialogue = true;
    170.                         characterVicinity = false;
    171.                         dialogueTrigger.enabled = false;
    172.                         theNPC.MoveRight();
    173.                     }
    174.                 }
    175.             }
    176.             else
    177.             {
    178.                 //The button prompt must be active in conjunction with the Space bar being pressed before the next line will show.
    179.                 if (buttonPrompts[3].activeSelf && Input.GetKeyUp(KeyCode.Space))
    180.                 {
    181.                     currentLine++;
    182.                     if (currentLine >= sentences.Length)
    183.                     {
    184.                         nameDisplay.enabled = false;
    185.                         dialogueBox.SetActive(false);
    186.                         dialogueActive = false;
    187.                         endDialogue = true;
    188.                         characterVicinity = false;
    189.                         dialogueTrigger.enabled = false;
    190.                         theNPC.MoveRight();
    191.                     }
    192.                 }
    193.             }
    194.         }
    195.         if (textDisplay.text == sentences[index] && SceneManagement.xbox360Controller == 1)
    196.         {
    197.             buttonPrompts[4].SetActive(true);
    198.             continueAllowed = true;
    199.             if (Input.GetKeyUp("joystick button 2"))
    200.             {
    201.                 continuePressed = true;
    202.                 continueAllowed = false;
    203.                 NextSentence();
    204.             }
    205.             else
    206.             {
    207.                 continuePressed = false;
    208.             }
    209.         }
    210.         else if (textDisplay.text == sentences[index] && SceneManagement.ps4Controller == 1)
    211.         {
    212.             buttonPrompts[5].SetActive(true);
    213.             continueAllowed = true;
    214.             if (Input.GetKeyUp("joystick button 0"))
    215.             {
    216.                 continuePressed = true;
    217.                 continueAllowed = false;
    218.                 NextSentence();
    219.             }
    220.             else
    221.             {
    222.                 continuePressed = false;
    223.             }
    224.         }
    225.         else if(textDisplay.text == sentences[index])
    226.         {
    227.             buttonPrompts[3].SetActive(true);
    228.             continueAllowed = true;
    229.             //Placing this 'if' statement here within the previous one will stop the button from being spammed.
    230.             if (Input.GetKeyUp(KeyCode.Space))
    231.             {
    232.                 continuePressed = true;
    233.                 continueAllowed = false;
    234.                 NextSentence();
    235.             }
    236.             else
    237.             {
    238.                 continuePressed = false;
    239.             }
    240.         }
    241.     }
    242.  
    243.     public void Hide()
    244.     {
    245.         buttonPrompts[0].SetActive(false);
    246.         buttonPrompts[1].SetActive(false);
    247.         buttonPrompts[2].SetActive(false);
    248.     }
    249.  
    250.     public void Xbox360Prompts()
    251.     {
    252.         buttonPrompts[1].SetActive(true);
    253.         Invoke("Hide", 3f);
    254.     }
    255.  
    256.     public void PS4Prompts()
    257.     {
    258.         buttonPrompts[2].SetActive(true);
    259.         Invoke("Hide", 3f);
    260.     }
    261.  
    262.     public void PCPrompts()
    263.     {
    264.         buttonPrompts[0].SetActive(true);
    265.         Invoke("Hide", 3f);
    266.     }
    267.  
    268.     public void ControllerDetection()
    269.     {
    270.         string[] names = Input.GetJoystickNames();
    271.         for (int x = 0; x < names.Length; x++)
    272.         {
    273.             //print(names[x].Length);
    274.             if (names[x].Length == 19)
    275.             {
    276.                 //print("PS4 CONTROLLER IS CONNECTED");
    277.                 pS4Controller = 1;
    278.                 xbox360Controller = 0;
    279.                 if (pS4Controller == 1)
    280.                 {
    281.                     //Debug.Log("PS4 controller detected");
    282.                 }
    283.             }
    284.             else if (names[x].Length == 33)
    285.             {
    286.                 //print("XBOX 360 CONTROLLER IS CONNECTED");
    287.                 pS4Controller = 0;
    288.                 xbox360Controller = 1;
    289.                 if (xbox360Controller == 1)
    290.                 {
    291.                     //Debug.Log("Xbox 360 controller detected");
    292.                 }
    293.             }
    294.             else
    295.             {
    296.                 pS4Controller = 0;
    297.                 xbox360Controller = 0;
    298.             }
    299.  
    300.             if (xbox360Controller == 0 && pS4Controller == 0)
    301.             {
    302.                 //Debug.Log("No controllers detected");
    303.             }
    304.         }
    305.     }
    306. }
    307.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6.  
    7. public class SceneManagement : MonoBehaviour
    8. {
    9.     public GameObject[] buttonPrompts;
    10.     public bool entranceVicinity;
    11.     public bool exitVicinity;
    12.     public string levelToLoad;
    13.     public static int xbox360Controller = 0;
    14.     public static int ps4Controller = 0;
    15.     public GameObject exitLight
    16.     {
    17.         get
    18.         {
    19.             if (_exitLight != null)
    20.                 return _exitLight;
    21.  
    22.             _exitLight = GameObject.Find("Exit Light");
    23.             return _exitLight;
    24.         }
    25.     }
    26.     public static bool insideHut;
    27.     public static bool outsideHut;
    28.     public static bool backOutsideHut;
    29.  
    30.     private GameObject _exitLight = null;
    31.     private PlayerController thePlayer;
    32.  
    33.     void Start()
    34.     {
    35.         thePlayer = FindObjectOfType<PlayerController>();
    36.         if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("start_area"))
    37.         {
    38.             outsideHut = true;
    39.             insideHut = false;
    40.             backOutsideHut = false;
    41.         }
    42.         if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("hut_interior"))
    43.         {
    44.             insideHut = true;
    45.             outsideHut = false;
    46.             backOutsideHut = false;
    47.             exitLight.SetActive(false);
    48.         }
    49.         if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("start_area 2"))
    50.         {
    51.             backOutsideHut = true;
    52.             outsideHut = false;
    53.             insideHut = false;
    54.         }
    55.     }
    56.  
    57.     public void OnTriggerEnter(Collider other)
    58.     {
    59.         if (outsideHut)
    60.         {
    61.             if (other.gameObject.CompareTag("Player"))
    62.             {
    63.                 entranceVicinity = true;
    64.                 exitVicinity = false;
    65.                 ControllerDetection();
    66.                 if (entranceVicinity && ps4Controller == 1)
    67.                 {
    68.                     PS4Prompts();
    69.                 }
    70.                 else if (entranceVicinity && xbox360Controller == 1)
    71.                 {
    72.                     Xbox360Prompts();
    73.                 }
    74.                 else
    75.                 {
    76.                     PCPrompts();
    77.                 }
    78.             }
    79.         }
    80.         if (Pickup.objectsDisabled && NPC.leverActivated && insideHut)
    81.         {
    82.             if (other.gameObject.CompareTag("Player"))
    83.             {
    84.                 entranceVicinity = false;
    85.                 exitVicinity = true;
    86.                 ControllerDetection();
    87.                 if (exitVicinity && ps4Controller == 1)
    88.                 {
    89.                     PS4Prompts();
    90.                 }
    91.                 else if (exitVicinity && xbox360Controller == 1)
    92.                 {
    93.                     Xbox360Prompts();
    94.                 }
    95.                 else
    96.                 {
    97.                     PCPrompts();
    98.                 }
    99.             }
    100.         }
    101.         else if(backOutsideHut)
    102.         {
    103.             if (other.gameObject.CompareTag("Player"))
    104.             {
    105.                 SceneManager.LoadScene(levelToLoad);
    106.             }
    107.         }
    108.     }
    109.  
    110.     public void OnTriggerExit(Collider other)
    111.     {
    112.         entranceVicinity = false;
    113.         exitVicinity = false;
    114.     }
    115.  
    116.     public void Update()
    117.     {
    118.         if (entranceVicinity)
    119.         {
    120.             if (xbox360Controller == 1)
    121.             {
    122.                 if (Input.GetKeyDown("joystick button 2"))
    123.                 {
    124.                     SceneManager.LoadScene(levelToLoad);
    125.                 }
    126.             }
    127.             else if (ps4Controller == 1)
    128.             {
    129.                 if (Input.GetKeyDown("joystick button 0"))
    130.                 {
    131.                     SceneManager.LoadScene(levelToLoad);
    132.                 }
    133.             }
    134.             else
    135.             {
    136.                 if (Input.GetKeyDown(KeyCode.Return))
    137.                 {
    138.                     SceneManager.LoadScene(levelToLoad);
    139.                 }
    140.             }
    141.         }
    142.         else if (exitVicinity)
    143.         {
    144.             if (xbox360Controller == 1)
    145.             {
    146.                 if (Input.GetKeyDown("joystick button 2"))
    147.                 {
    148.                     SceneManager.LoadScene(levelToLoad);
    149.                 }
    150.             }
    151.             else if (ps4Controller == 1)
    152.             {
    153.                 if (Input.GetKeyDown("joystick button 0"))
    154.                 {
    155.                     SceneManager.LoadScene(levelToLoad);
    156.                 }
    157.             }
    158.             else
    159.             {
    160.                 if (Input.GetKeyDown(KeyCode.Return))
    161.                 {
    162.                     SceneManager.LoadScene(levelToLoad);
    163.                 }
    164.             }
    165.         }
    166.  
    167.         if (Pickup.objectsDisabled && NPC.leverActivated && insideHut)
    168.         {
    169.             exitLight.SetActive(true);
    170.         }
    171.     }
    172.  
    173.     public void Hide()
    174.     {
    175.         buttonPrompts[0].SetActive(false);
    176.         buttonPrompts[1].SetActive(false);
    177.         buttonPrompts[2].SetActive(false);
    178.     }
    179.  
    180.     public void Xbox360Prompts()
    181.     {
    182.         buttonPrompts[1].SetActive(true);
    183.         Invoke("Hide", 3f);
    184.     }
    185.  
    186.     public void PS4Prompts()
    187.     {
    188.         buttonPrompts[2].SetActive(true);
    189.         Invoke("Hide", 3f);
    190.     }
    191.  
    192.     public void PCPrompts()
    193.     {
    194.         buttonPrompts[0].SetActive(true);
    195.         Invoke("Hide", 3f);
    196.     }
    197.  
    198.     public void ControllerDetection()
    199.     {
    200.         string[] names = Input.GetJoystickNames();
    201.         for (int x = 0; x < names.Length; x++)
    202.         {
    203.             //print(names[x].Length);
    204.             if (names[x].Length == 19)
    205.             {
    206.                 //print("PS4 CONTROLLER IS CONNECTED");
    207.                 ps4Controller = 1;
    208.                 xbox360Controller = 0;
    209.                 if (ps4Controller == 1)
    210.                 {
    211.                     //Debug.Log("PS4 controller detected");
    212.                 }
    213.             }
    214.             else if (names[x].Length == 33)
    215.             {
    216.                 //print("XBOX 360 CONTROLLER IS CONNECTED");
    217.                 ps4Controller = 0;
    218.                 xbox360Controller = 1;
    219.                 if (xbox360Controller == 1)
    220.                 {
    221.                     //Debug.Log("Xbox 360 controller detected");
    222.                 }
    223.             }
    224.             else
    225.             {
    226.                 ps4Controller = 0;
    227.                 xbox360Controller = 0;
    228.             }
    229.  
    230.             if(xbox360Controller == 0 && ps4Controller == 0)
    231.             {
    232.                 //Debug.Log("No controllers detected");
    233.             }
    234.         }
    235.     }
    236. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    The easiest way to get a handle on this is going to be to add text overlays to the screen that display your "lockout" variables, the things that you intend to prevent you from doing one thing when you're doing the other.

    Then while playing you can debug exactly which one is not doing what you expect it to do. Without the fully-functioning project, I doubt anyone here can look at this and say "This is the bad part right here." It's completely dependent on what scripts are driving what in your scene.
     
  3. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    529
    Sorry for the late response. I'm not sure what you mean by text overlays to the screen. I'm only familiar with using Debug.Log and Print to add some text to the console to see if that part of the script is being read. Apart from that, I'm not sure how I'd be able to pinpoint the issue. :-\
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Use the Unity UI components, such as UnityEngine.UI.Text, etc., as you would with all the rest of your UI.

    Or just use the OnGUI() method, since it's just for debugging.
     
  5. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    529
    So, what you're saying is, I should create and enable text to display in the game for when each thing happens? And whichever text pop-up doesn't show, that's the culprit...?