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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Multiple Button Presses Not Working

Discussion in 'Scripting' started by Dakotalogy, Jun 13, 2020.

  1. Dakotalogy

    Dakotalogy

    Joined:
    Jun 13, 2020
    Posts:
    3
    Hello,

    I'm trying to rotate a sprite along the z axis using keyboard input and input actions. The action for all of them is set to pass through and the control type is button. The composite type for each is 1D Axis and the min/max values are -1 and 1 respectively. When I code my script to implement the rotation, the keys work individually. The problem I am having is wanting to press two keys at once. Only the first switch statement is able to rotate the sprite when pressing two keys and the second switch doesn't function the same. When I put one switch above the other one the problem reverses. What ways can I solve this so that both switch statements allow multiple keypresses?



    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6.  
    7. public class PlayerController : MonoBehaviour {
    8.     private PlayerControls playerControls;
    9.     private float xRightInput, yRightInput;
    10.  
    11.  
    12.     private void Awake() {
    13.         playerControls = new PlayerControls();
    14.     }
    15.  
    16.     private void OnEnable() {
    17.         playerControls.Enable();
    18.     }
    19.  
    20.     private void OnDisable() {
    21.         playerControls.Disable();
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update() {
    26.         GetPlayerInput();
    27.     }
    28.  
    29.     //Read the movement value
    30.     private void GetPlayerInput() {
    31.  
    32.         xRightInput = playerControls.Movement.HorizontalRotation.ReadValue<float>();
    33.         yRightInput = playerControls.Movement.VerticalRotation.ReadValue<float>();
    34.  
    35.     }
    36.  
    37.     private void FixedUpdate() {
    38.  
    39.         //Horizontal & Vertical Transformations
    40.         switch (yRightInput) {
    41.             case -1:
    42.                 DownRotate();
    43.                 break;
    44.             case 1:
    45.                 UpRotate();
    46.                 break;
    47.             default:
    48.                 break;
    49.         }
    50.         switch (xRightInput) {
    51.             case -1:
    52.                 LeftRotate();
    53.                 break;
    54.             case 1:
    55.                 RightRotate();
    56.                 break;
    57.             default:
    58.                 break;
    59.         }
    60.     }
    61.  
    62.     private void LeftRotate() {
    63.         transform.rotation = Quaternion.Euler(0, 0, 90);
    64.  
    65.         if (yRightInput == -1) {
    66.             transform.rotation = Quaternion.Euler(0, 0, 180);
    67.         } else if (yRightInput == 1) {
    68.             transform.rotation = Quaternion.Euler(0, 0, 0);
    69.         }
    70.     }
    71.  
    72.     private void RightRotate() {
    73.         transform.rotation = Quaternion.Euler(0, 0, 270);
    74.  
    75.         if (yRightInput == -1) {
    76.             transform.rotation = Quaternion.Euler(0, 0, 180);
    77.         } else if (yRightInput == 1) {
    78.             transform.rotation = Quaternion.Euler(0, 0, 0);
    79.         }
    80.     }
    81.  
    82.     private void DownRotate() {
    83.         transform.rotation = Quaternion.Euler(0, 0, 180);
    84.  
    85.         if (xRightInput == -1) {
    86.             transform.rotation = Quaternion.Euler(0, 0, 90);
    87.         } else if (xRightInput == 1) {
    88.             transform.rotation = Quaternion.Euler(0, 0, -90);
    89.         }
    90.     }
    91.  
    92.     private void UpRotate() {
    93.         transform.rotation = Quaternion.Euler(0, 0, 0);
    94.  
    95.         if (xRightInput == -1) {
    96.             transform.rotation = Quaternion.Euler(0, 0, 90);
    97.         } else if (xRightInput == 1) {
    98.             transform.rotation = Quaternion.Euler(0, 0, -90);
    99.         }
    100.  
    101.     }
    102.  
    103. }
    104.  
     
    Last edited: Jun 13, 2020
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    What kind of view is this? Why not just calc a rotation from the vectors and set the rotation once?
     
  3. Dakotalogy

    Dakotalogy

    Joined:
    Jun 13, 2020
    Posts:
    3
    It's a 2D scene. How would I go about doing that?
     
  4. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    What is your desired movement like? It is top-down?
     
  5. Dakotalogy

    Dakotalogy

    Joined:
    Jun 13, 2020
    Posts:
    3
    Yes it is top down. I was able to change the code to calculating the rotation based off a vector, but I would like to remove the 45 degree inbetween when you press two keys and just have it look in the cardinal directions.

    Code (CSharp):
    1. public class PlayerController : MonoBehaviour {
    2.  
    3.     private Vector2 rightInput;
    4.  
    5.     private void GetPlayerInput() {
    6.         rightInput = new Vector2(xRightInput, yRightInput);
    7.     }
    8.  
    9.     private void FixedUpdate() {
    10.         Vector3 currentRotation = Vector3.left * rightInput.x + Vector3.down * rightInput.y;
    11.         Quaternion playerRotation = Quaternion.LookRotation(currentRotation, Vector3.forward);
    12.  
    13.         rigidb.SetRotation(playerRotation); //this is the Rigidbody2D
    14.     }
    15. }
     
    Last edited: Jun 13, 2020
  6. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    So you only want it to rotate to north, south, east, and west? You could take your playerRotation quaternion and snap it to the nearest 90 degrees. You would probably want conditions to decide which way to face when both are pressed though.