Search Unity

Turn Character with Touch Swipe

Discussion in 'Scripting' started by Artoons, May 30, 2021.

  1. Artoons

    Artoons

    Joined:
    May 30, 2021
    Posts:
    3
    So I am making this game where the character is stationary and it can rotate around 360 degrees in any direction and I'm making this game for the android platform.

    I have made a script by watching some videos and going through the forums. it is working but not quite right.

    This is my script

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Main : MonoBehaviour
    4. {
    5.     private Touch initTouch = new Touch();
    6.     public float speed = 0.02f;
    7.  
    8.     private float rotX = 0f;
    9.     private float rotY = 0f;
    10.  
    11.     public float direction = -1;
    12.  
    13.  
    14.     void Start()
    15.     {
    16.         rotX = transform.rotation.x;
    17.         rotY = transform.rotation.y;
    18.     }
    19.     void Update()
    20.     {
    21.         foreach (Touch touch in Input.touches)
    22.         {
    23.  
    24.             if (touch.phase == TouchPhase.Began)
    25.             {
    26.                 initTouch = touch;
    27.             }
    28.             else if (touch.phase == TouchPhase.Moved)
    29.             {
    30.                 float deltaX = initTouch.position.x - touch.position.x;
    31.                 float deltaY = initTouch.position.y - touch.position.y;
    32.                 rotX -= deltaY * Time.deltaTime * speed * direction;
    33.                 rotY += deltaX * Time.deltaTime * speed * direction;
    34.                 transform.rotation = Quaternion.Euler(rotX, rotY, 0f);
    35.             }
    36.             else if (touch.phase == TouchPhase.Ended)
    37.             {
    38.                 initTouch = new Touch();
    39.             }
    40.         }
    41.     }
    what this should do is when I drag my finger along the screen the player should turn in the direction of my finger but when I drag the finger along a direction and drag it back without lifting the finger the object still keeps rotating in the same direction

    Idk if this is caused by my mobile device or if something is wrong with the code

    I have some experience in unity but I don't have that much knowledge in coding

    Pls let me know if I can change anything to fix this or use another way to do this
     
    Last edited: May 30, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Your problem begins with line 16 and line 17. Those .x and .y fields are NOT degrees. You cannot interact with Quaternions that way. Those are internal fields.

    Looking at the rest of your code, are you just looking for a "virtual analog joystick?" It sorta looks like that is what's going on, based on where you take the initTouch and subtract the current touch.

    If so, you're welcome to use or refer to my VAButton class in my proximity_buttons project. Check out the TwinStick Shooter demo in there.

    proximity_buttons is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/proximity_buttons

    https://github.com/kurtdekker/proximity_buttons

    https://gitlab.com/kurtdekker/proximity_buttons

    https://sourceforge.net/projects/proximity-buttons/
     
    Artoons likes this.
  3. Artoons

    Artoons

    Joined:
    May 30, 2021
    Posts:
    3
    Thank you so much for the reply. Wasn't expecting it to be that fast.

    I don't think a Virtual joystick is what I want (pls correct me if I am wrong) because with a joystick when the finger is dragged and stopped object still keeps rotating.

    I want mine to be like the movement of an fps game (looking around) so that after the finger is dragged the rotation should stop.

    I watched some videos on fps movement but most of them are for windows and most don't make sense.

    As for lines 16 & 17, I am not sure what I should do. If you can explain it a bit more I would appreciate that and thanks again for the reply.

    I think my best bet would be to copy an fps movement script and remove the unnecessary parts.

    Any more ideas?
     
    Last edited: May 30, 2021
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    I would refine that further:

    - start with an FPS look script
    - figure out how to inject moveX / moveY inputs into it (eg, pluck out the mouse and make a mouse module that drives the input.
    - finally make a touch module that watches a portion of the screen and feeds the above inputs.
     
    Artoons likes this.