Search Unity

Can no longer control game with a controller and keyboard

Discussion in 'Scripting' started by DustyShinigami, Jun 19, 2019.

  1. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    529
    I've no idea what's happened, but for some weird reason, I can longer control my character with the keyboard when the controller is plugged in. This has never been an issue before, and in some scenes, because I haven't got the controller and prompts set up yet, I can and have to use both the keyboard and controller. And yet I can still move the player with arrows if my controller is plugged in. Specifically, it's the jump and fire keys that don't respond. Using the debug log doesn't print anything to the console when they're pressed either.

    These are two scripts that are related anyway. Maybe someone will spot something I'm overlooking...

    PlayerController:

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

    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.     public static bool insideHut;
    17.     public static bool outsideHut;
    18.     public static bool backOutsideHut;
    19.  
    20.     private PlayerController thePlayer;
    21.  
    22.     void Start()
    23.     {
    24.         thePlayer = FindObjectOfType<PlayerController>();
    25.         if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("start_area"))
    26.         {
    27.             outsideHut = true;
    28.             insideHut = false;
    29.             backOutsideHut = false;
    30.         }
    31.         if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("hut_interior"))
    32.         {
    33.             insideHut = true;
    34.             outsideHut = false;
    35.             backOutsideHut = false;
    36.             exitLight.SetActive(false);
    37.         }
    38.         if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("start_area 2"))
    39.         {
    40.             backOutsideHut = true;
    41.             outsideHut = false;
    42.             insideHut = false;
    43.         }
    44.     }
    45.  
    46.     public void OnTriggerEnter(Collider other)
    47.     {
    48.         if (outsideHut)
    49.         {
    50.             if (other.gameObject.CompareTag("Player"))
    51.             {
    52.                 entranceVicinity = true;
    53.                 exitVicinity = false;
    54.                 ControllerDetection();
    55.                 if (entranceVicinity && ps4Controller == 1)
    56.                 {
    57.                     PS4Prompts();
    58.                 }
    59.                 else if (entranceVicinity && xbox360Controller == 1)
    60.                 {
    61.                     Xbox360Prompts();
    62.                 }
    63.                 else
    64.                 {
    65.                     PCPrompts();
    66.                 }
    67.             }
    68.         }
    69.         if (Pickup.objectsDisabled && NPC.leverActivated && insideHut)
    70.         {
    71.             if (other.gameObject.CompareTag("Player"))
    72.             {
    73.                 entranceVicinity = false;
    74.                 exitVicinity = true;
    75.                 ControllerDetection();
    76.                 if (exitVicinity && ps4Controller == 1)
    77.                 {
    78.                     PS4Prompts();
    79.                 }
    80.                 else if (exitVicinity && xbox360Controller == 1)
    81.                 {
    82.                     Xbox360Prompts();
    83.                 }
    84.                 else
    85.                 {
    86.                     PCPrompts();
    87.                 }
    88.             }
    89.         }
    90.         else if(backOutsideHut)
    91.         {
    92.             if (other.gameObject.CompareTag("Player"))
    93.             {
    94.                 SceneManager.LoadScene(levelToLoad);
    95.             }
    96.         }
    97.     }
    98.  
    99.     public void OnTriggerExit(Collider other)
    100.     {
    101.         entranceVicinity = false;
    102.         exitVicinity = false;
    103.     }
    104.  
    105.     public void Update()
    106.     {
    107.         if (entranceVicinity)
    108.         {
    109.             if (xbox360Controller == 1)
    110.             {
    111.                 if (Input.GetKeyDown("joystick button 2"))
    112.                 {
    113.                     SceneManager.LoadScene(levelToLoad);
    114.                 }
    115.             }
    116.             else if (ps4Controller == 1)
    117.             {
    118.                 if (Input.GetKeyDown("joystick button 0"))
    119.                 {
    120.                     SceneManager.LoadScene(levelToLoad);
    121.                 }
    122.             }
    123.             else
    124.             {
    125.                 if (Input.GetKeyDown(KeyCode.Return))
    126.                 {
    127.                     SceneManager.LoadScene(levelToLoad);
    128.                 }
    129.             }
    130.         }
    131.         else if (exitVicinity)
    132.         {
    133.             if (xbox360Controller == 1)
    134.             {
    135.                 if (Input.GetKeyDown("joystick button 2"))
    136.                 {
    137.                     SceneManager.LoadScene(levelToLoad);
    138.                 }
    139.             }
    140.             else if (ps4Controller == 1)
    141.             {
    142.                 if (Input.GetKeyDown("joystick button 0"))
    143.                 {
    144.                     SceneManager.LoadScene(levelToLoad);
    145.                 }
    146.             }
    147.             else
    148.             {
    149.                 if (Input.GetKeyDown(KeyCode.Return))
    150.                 {
    151.                     SceneManager.LoadScene(levelToLoad);
    152.                 }
    153.             }
    154.         }
    155.  
    156.         if (Pickup.objectsDisabled && NPC.leverActivated && insideHut)
    157.         {
    158.             exitLight.SetActive(true);
    159.         }
    160.     }
    161.  
    162.     public void Hide()
    163.     {
    164.         buttonPrompts[0].SetActive(false);
    165.         buttonPrompts[1].SetActive(false);
    166.         buttonPrompts[2].SetActive(false);
    167.     }
    168.  
    169.     public void Xbox360Prompts()
    170.     {
    171.         buttonPrompts[1].SetActive(true);
    172.         Invoke("Hide", 3f);
    173.     }
    174.  
    175.     public void PS4Prompts()
    176.     {
    177.         buttonPrompts[2].SetActive(true);
    178.         Invoke("Hide", 3f);
    179.     }
    180.  
    181.     public void PCPrompts()
    182.     {
    183.         buttonPrompts[0].SetActive(true);
    184.         Invoke("Hide", 3f);
    185.     }
    186.  
    187.     public void ControllerDetection()
    188.     {
    189.         string[] names = Input.GetJoystickNames();
    190.         for (int x = 0; x < names.Length; x++)
    191.         {
    192.             //print(names[x].Length);
    193.             if (names[x].Length == 19)
    194.             {
    195.                 //print("PS4 CONTROLLER IS CONNECTED");
    196.                 ps4Controller = 1;
    197.                 xbox360Controller = 0;
    198.                 if (ps4Controller == 1)
    199.                 {
    200.                     //Debug.Log("PS4 controller detected");
    201.                 }
    202.             }
    203.             else if (names[x].Length == 33)
    204.             {
    205.                 //print("XBOX 360 CONTROLLER IS CONNECTED");
    206.                 ps4Controller = 0;
    207.                 xbox360Controller = 1;
    208.                 if (xbox360Controller == 1)
    209.                 {
    210.                     //Debug.Log("Xbox 360 controller detected");
    211.                 }
    212.             }
    213.             else
    214.             {
    215.                 ps4Controller = 0;
    216.                 xbox360Controller = 0;
    217.             }
    218.  
    219.             if(xbox360Controller == 0 && ps4Controller == 0)
    220.             {
    221.                 //Debug.Log("No controllers detected");
    222.             }
    223.         }
    224.     }
    225. }
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    Notice that your attack code is structured like
    Code (csharp):
    1.  
    2. if (SceneManagement.xbox360Controller == 1)
    3. { ...
    4. }
    5. else if (SceneManagement.ps4Controller == 1)
    6. { ...
    7. }
    8. else
    9.                     {
    10.                         if (Input.GetKeyDown(KeyCode.Return))
    11.                         { ...
    12.  
    Due to your chain of if/else if/else it's only ever going to do that keyboard check if xbox360Controller and ps4Controller are both not equal to 1.
     
  3. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    529
    That doesn't make sense though because surely in this case the PS4 controller isn't equal to 1...? It detects the Xbox 360 controller and that's the only one that comes back as true. I would have thought that if one of the controllers comes back as 1/true, the keyboard won't be and will be ignored...? If I do two if statements and an else, again, only one controller is true (the 360 controller), and yet I can use the keyboard as well. o_O
     
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    Yes, the keyboard is ignored. That's what I said. Isn't that what you are complaining about, though? In your first post you are asking why the xbox controller and keyboard don't work at the same time, right? Any time you have
    Code (csharp):
    1.  
    2. if (...)
    3. { Do A
    4. }
    5. else if (...)
    6. { Do B
    7. }
    8. else
    9. { Do C
    10. }
    11.  
    then only one of those things will ever happen.
    Ok, so problem solved?
     
  5. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    529
    Well, it is solved, yeah, but I'm just trying to understand the logic behind it. If I have an if statement for the keyboard controls first, and then the controller code are both in 'else if' statements, surely by what you've said, only the keyboard would work and the controllers would be ignored...? But the one I have plugged in isn't being ignored.
     
  6. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    Well, correct me if I'm wrong, but your controller checks are checking if the player has the controller plugged-in or something like that, right? You are checking if the player is using the controller in general, so if the player is using an xbox 360 controller, then that is going to be "1" all of the time.

    The keyboard check is different, though, because there you are checking getKeyDown, which will only ever be true in that one frame where a keypress occurs. So, if you manage to press the key and press the controller button within the exact same 0.016 second window then you should find that only the key press will work if you have the if (getKeyDown...) else (controller) bla bla bla...