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

Help needed on 2D actor rotation upon touch input

Discussion in 'Scripting' started by mhedges, Feb 23, 2017.

  1. mhedges

    mhedges

    Joined:
    Feb 23, 2017
    Posts:
    13
    Hello -

    First post in Unity Forum. Firstly, allow myself to introduce... myself. I have built a number of games in another game engine, GameSalad. I decided it was time to jump to another language, and thought it was best to come to Unity.

    While looking at tutorials, I thought it would be good to also port one of my existing games to Unity. The game I chose is inspired by the arcade classic Asteroids, so you should have a sense of the objective and gameplay.

    I have four buttons - left, right, thrust, fire. The keyboard input works just fine - the ship rorates, thruts forward or fires accordingly. However, I am stuck on touch input, and have not yet found the right thread, blog or youtube video yet. Specifically, when I press on the left button, the ship should rotate left (counterclockwise). But as I currently stand, if you press the left button, the ship does not constantly rotate, but just by x degrees, thus I would need to tap left again.

    I have a button script and a player control script:
    • The button script sets Button01 to true if the button is pressed.
    • The player control script invokes the transform.Rotate behavior.
    Both are enclosed within "void Update" in their own scripts to update once per frame.

    Any ideas as to what may impede the constant rotation?

    Thanks, regards,
    Marcos
     
  2. SyncomGroup

    SyncomGroup

    Joined:
    May 26, 2014
    Posts:
    6
    Would be helpfull if you would post your code , but I am guessing you are not not multiplying your value with Time.deltaTime
    Code (CSharp):
    1. // V1
    2.     public delegate bool ShouldRotate();
    3.     public ShouldRotate OnRotateLeft;
    4.  
    5.     //V2
    6.     public delegate float GetRotationInput();
    7.     public GetRotationInput OnGetRotation ;
    8.  
    9.     public Vector3 dir;
    10.     public float speed;
    11.  
    12.     public void Update()
    13.     {
    14.         //Version 1
    15.         if (OnRotateLeft ())
    16.             transform.eulerAngles += speed * dir * Time.deltaTime;
    17.  
    18.         //Version 2
    19.         transform.eulerAngles += OnGetRotation() * speed * dir * Time.deltaTime;
    20.     }
    a example would be like this
     
    Last edited: Feb 23, 2017
  3. mhedges

    mhedges

    Joined:
    Feb 23, 2017
    Posts:
    13
    Yes, I should have posted the code. This is the existing code.

    In the Player Ship Control I have the following:
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerShipControl : MonoBehaviour {
    6.  
    7. [INDENT]private Rigidbody2D rb;[/INDENT]
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.         rb = GetComponent<Rigidbody2D> ();
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     void Update () {
    16.         if (TouchJoyPad.Button01) {
    17.             transform.Rotate (0, 0, 20);
    18.         }
    19.     }
    20. }
    21.  

    In the Button Script I have the following:
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5.  
    6. public class TouchJoyPad : MonoBehaviour {
    7.  
    8.     //Variables that can be referenced from outside
    9.     static public bool
    10.     Button01, Button01Down, Button01Up, Button02, Button02Down, Button02Up,     Button03, Button03Down, Button03Up, Button04, Button04Down, Button04Up;
    11.  
    12.     //Variable for internal processing
    13.     bool PreButton01, PreButton02, PreButton03, PreButton04;
    14.     Vector2[] convertedTouchPos;
    15.     Vector2 JoyKeyPos2D, DragArea2D, Button01Pos2D, Button02Pos2D, Button03Pos2D, Button04Pos2D;
    16.     RectTransform rectJoyKey, rectDragArea, rectButton01, rectButton02, rectButton03, rectButton04;
    17.  
    18.     // Use this for initialization
    19.     void Start () {
    20.         //Detect the rect of Joykey and buttons
    21.         rectButton01 = GameObject.Find ("Image_Button01").GetComponent<RectTransform> ();
    22.         rectButton02 = GameObject.Find ("Image_Button02").GetComponent<RectTransform> ();
    23.         rectButton03 = GameObject.Find ("Image_Button03").GetComponent<RectTransform> ();
    24.         rectButton04 = GameObject.Find ("Image_Button04").GetComponent<RectTransform> ();
    25.  
    26.         Button01Pos2D = new Vector2 (rectButton01.position.x, rectButton01.position.y);
    27.         Button02Pos2D = new Vector2 (rectButton02.position.x, rectButton02.position.y);
    28.         Button03Pos2D = new Vector2 (rectButton03.position.x, rectButton03.position.y);
    29.         Button04Pos2D = new Vector2 (rectButton04.position.x, rectButton04.position.y);
    30.  
    31.         ScreenWidth = Screen.width;
    32.  
    33.         //Boolean value initialization for the state detection
    34.         Button01 = false;
    35.         Button02 = false;
    36.         Button03 = false;
    37.         Button04 = false;
    38.     }
    39.    
    40.     // Update is called once per frame
    41.     void Update () {
    42.  
    43.         if (ScreenWidth != Screen.width) {
    44.             Button01Pos2D = new Vector2 (rectButton01.position.x, rectButton01.position.y);
    45.             Button02Pos2D = new Vector2 (rectButton02.position.x, rectButton02.position.y);
    46.             Button03Pos2D = new Vector2 (rectButton03.position.x, rectButton03.position.y);
    47.             Button04Pos2D = new Vector2 (rectButton04.position.x, rectButton04.position.y);
    48.  
    49.             ScreenWidth = Screen.width;
    50.         }
    51.  
    52.         //Boolean value initialization for the state detection
    53.         Button01 = false;
    54.         Button02 = false;
    55.         Button03 = false;
    56.         Button04 = false;
    57.  
    58. //        Detect touches
    59.         if (Input.touchCount > 0) {
    60.             convertedTouchPos = new Vector2[Input.touchCount];
    61.             for (int i = 0; i < Input.touchCount; i++) {
    62.                 convertedTouchPos [i] = Input.GetTouch (i).position;
    63.  
    64. //            Processing of the Button01
    65.                     Button01 = true;
    66.                 } else if (convertedTouchPos[i].x > Button02Pos2D.x - rectButton02.rect.width * 0.50f
    67.                     && convertedTouchPos[i].x < Button02Pos2D.x + rectButton02.rect.width * 0.50f
    68.                     && convertedTouchPos[i].y > Button02Pos2D.y - rectButton02.rect.height * 0.50f
    69.                     && convertedTouchPos[i].y < Button02Pos2D.y + rectButton02.rect.height * 0.50f) {
    70. //            Processing of the Button02
    71.                     Button02 = true;
    72.                 } else if (convertedTouchPos[i].x > Button03Pos2D.x - rectButton03.rect.width * 0.50f
    73.                     && convertedTouchPos[i].x < Button03Pos2D.x + rectButton03.rect.width * 0.50f
    74.                     && convertedTouchPos[i].y > Button03Pos2D.y - rectButton03.rect.height * 0.50f
    75.                     && convertedTouchPos[i].y < Button03Pos2D.y + rectButton03.rect.height * 0.50f) {
    76. //            Processing of the Button03
    77.                     Button03 = true;
    78.                 } else if (convertedTouchPos[i].x > Button04Pos2D.x - rectButton04.rect.width * 0.5f
    79.                     && convertedTouchPos[i].x < Button04Pos2D.x + rectButton04.rect.width * 0.5f
    80.                     && convertedTouchPos[i].y > Button04Pos2D.y - rectButton04.rect.height * 0.5f
    81.                     && convertedTouchPos[i].y < Button04Pos2D.y + rectButton04.rect.height * 0.5f) {
    82. //            Processing of the Button04
    83.                     Button04 = true;
    84.                 } else if (convertedTouchPos[i].x > DragArea2D.x - rectDragArea.rect.width * 0.5f
    85.                     && convertedTouchPos[i].x < DragArea2D.x + rectDragArea.rect.width * 0.5f
    86.                     && convertedTouchPos[i].y > DragArea2D.y - rectDragArea.rect.height * 0.5f
    87.                     && convertedTouchPos[i].y < DragArea2D.y + rectDragArea.rect.height * 0.5f)
    88.             }
    89.         }
    90.  
    91.         if (Button01 != PreButton01) {
    92.             if (Button01) {
    93.                 Button01Down = true;
    94.             } else {
    95.                 Button01Up = true;
    96.             }
    97.         } else {
    98.             Button01Down = false;
    99.             Button01Up = false;
    100.         }
    101.  
    102.         if (Button02 != PreButton02) {
    103.             if (Button02) {
    104.                 Button02Down = true;
    105.             } else {
    106.                 Button02Up = true;
    107.             }
    108.         } else {
    109.             Button02Down = false;
    110.             Button02Up = false;
    111.         }
    112.  
    113.         if (Button03 != PreButton03) {
    114.             if (Button03) {
    115.                 Button03Down = true;
    116.             } else {
    117.                 Button03Up = true;
    118.             }
    119.         } else {
    120.             Button03Down = false;
    121.             Button03Up = false;
    122.         }
    123.  
    124.         if (Button04 != PreButton04) {
    125.             if (Button04) {
    126.                 Button04Down = true;
    127.             } else {
    128.                 Button04Up = true;
    129.             }
    130.         } else {
    131.             Button04Down = false;
    132.             Button04Up = false;
    133.         }
    134.  
    135. //        To record the state value of this frame
    136.         PreButton01 = Button01;
    137.         PreButton02 = Button02;
    138.         PreButton03 = Button03;
    139.         PreButton04 = Button04;
    140.     }
    141. }
    142.  
     
  4. mhedges

    mhedges

    Joined:
    Feb 23, 2017
    Posts:
    13
    Solved! Changed the void Update () routine in the Player Ship Control script to:

    Code (CSharp):
    1.     void Update () {
    2.  
    3.         if (TouchJoyPad.Button01) {
    4.             transform.Rotate (0, 0, rotationspeed);
    5.         }
    6.     }
    Added a public float rotationspeed of about 7.5f. Thanks for the help!