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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[SOLVED] Character moves in direction of swipe touch until finger is lifted.

Discussion in '2D' started by cliffman1992, Apr 15, 2015.

  1. cliffman1992

    cliffman1992

    Joined:
    Mar 3, 2014
    Posts:
    5
    As the title says, I would like to know if anyone could give me a sample script of how to move something in the direction of a swipe and keep moving that direction until I lift my finger. For example, I touch the screen and swipe right, the character will move right until I lift my finger up. The script I currently have, I'm using Input.GetTouch(0) to detect swipe direction, but I have to keep moving my finger to the start of the screen and swipe it across. Thanks in advance for any help.
     
  2. cliffman1992

    cliffman1992

    Joined:
    Mar 3, 2014
    Posts:
    5
    I finally figured out how to accomplish what I wanted to do. I've put the full script in this comment in case anyone else comes by and wants to use it. It will work for any 2D game, and accepts keyboard or mobile input. It started as the stock FPSInputController, I modified it to do what I needed. Feel free to use in your own game if you would like.


    Code (JavaScript):
    1.  
    2. private var motor : CharacterMotor;
    3.  
    4. //variable to allow me to disable movement quickly
    5. static var movement : boolean = true;
    6.  
    7. //variable allowing the ability to alter the sensitivity of touch direction change
    8. public var sensitivity : double = 1.5;
    9.  
    10.  
    11.  
    12.  
    13. // Use this for initialization
    14. function Awake () {
    15.     motor = GetComponent(CharacterMotor);
    16. }
    17.  
    18.  
    19.  
    20. //boolean variable used to testing of direction later
    21. var mobileRight : boolean;
    22.  
    23. // Update is called once per frame
    24. function Update () {
    25.  
    26.    
    27.    
    28.         // Get the input vector from keyboard or analog stick
    29.         var directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
    30.    
    31.  
    32.  
    33.         //if no keyboard input... use mobile controls
    34.         if(directionVector == Vector3.zero){
    35.  
    36.            
    37.             //check for moved or stationary finger
    38.             if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved || Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Stationary) {
    39.                
    40.                 //check for change in direction every frame
    41.                 var touchDeltaPosition : Vector2 = Input.GetTouch(0).deltaPosition;
    42.                
    43.                 //if direction is greater than sensitivity (1.5), set the movement to right, also set mobileRight to true... this will allow movement with stationary finger
    44.                 if(touchDeltaPosition.x > sensitivity){
    45.  
    46.                     //set movement right
    47.                     directionVector = Vector3.right;
    48.  
    49.                     //allows for movement after finger stops moving
    50.                     mobileRight = true;
    51.                 }
    52.  
    53.                 //else check to see if direction of finger movement is less than -sensitivity (-1.5) if so set direction to left and mobileRight to false
    54.                 else if(touchDeltaPosition.x < -sensitivity){
    55.                    
    56.                     //set direction to left
    57.                     directionVector = Vector3.left;
    58.  
    59.                     // set mobileRight to false allowing later testing
    60.                     mobileRight = false;
    61.                 }
    62.  
    63.                 //if touch direction is 0 (Finger NOT moving)
    64.                 else if(touchDeltaPosition.x == 0){
    65.  
    66.                     //check to see if last direction was right,... if so move right while finger direction is 0
    67.                     if(mobileRight){
    68.                         directionVector = Vector3.right;
    69.                     }
    70.  
    71.                     //if last direction was left move left as above
    72.                     else{
    73.                         directionVector = Vector3.left;
    74.                     }
    75.                 }
    76.             }
    77.                    
    78.         }
    79.        
    80.            
    81.        
    82.         //code for other part of game... allows me to disable movement quickly
    83.         if(!movement){
    84.             directionVector = Vector3.zero;
    85.        
    86.         }
    87.    
    88.  
    89.  
    90.         if (directionVector != Vector3.zero) {
    91.             // Get the length of the directon vector and then normalize it
    92.             // Dividing by the length is cheaper than normalizing when we already have the length anyway
    93.             var directionLength = directionVector.magnitude;
    94.             directionVector = directionVector / directionLength;
    95.        
    96.             // Make sure the length is no bigger than 1
    97.             directionLength = Mathf.Min(1, directionLength);
    98.        
    99.             // Make the input vector more sensitive towards the extremes and less sensitive in the middle
    100.             // This makes it easier to control slow speeds when using analog sticks
    101.             directionLength = directionLength * directionLength;
    102.        
    103.             // Multiply the normalized direction vector by the modified length
    104.             directionVector = directionVector * directionLength;
    105.         }
    106.    
    107.         // Apply the direction to the CharacterMotor
    108.            motor.inputMoveDirection = transform.rotation * directionVector;
    109.        
    110.         //accepts mobile tap of first or second touch, space, or click right and left to make character jump
    111.             if(movement && Input.GetKeyDown (KeyCode.Space) || movement && Input.GetMouseButtonDown(0) || movement && Input.GetMouseButtonDown(1)){
    112.             motor.inputJump = true;
    113.             //motor.inputJump = Input.GetButton("Jump");
    114.         }
    115.         else{
    116.             motor.inputJump = false;
    117.         }
    118.    
    119. }
    120.  
    121. // Require a character controller to be attached to the same game object
    122. @script RequireComponent (CharacterMotor)
    123. @script AddComponentMenu ("Character/FPS Input Controller")
    124.  
     
  3. Chintan-Kansagara

    Chintan-Kansagara

    Joined:
    Jul 16, 2016
    Posts:
    19
    HI
    my problem is 2d character move in input.mousePosition
    but next to other ui part to touch than move to character touch direction
    only first touch direction to move not a second ui touch to move in my charecter
    plz help me.

    Vector3 Target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    Target.z = transform.position.z;
    transform.position = Vector3.MoveTowards(transform.position, Target, mouseModeSpeed * Time.deltaTime);


    other ui to touch not character direction change default direction to movement continue.