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

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.

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.

private var motor : CharacterMotor;

//variable to allow me to disable movement quickly
static var movement : boolean = true;

//variable allowing the ability to alter the sensitivity of touch direction change
public var sensitivity : double = 1.5;




// Use this for initialization
function Awake () {
    motor = GetComponent(CharacterMotor);
}



//boolean variable used to testing of direction later
var mobileRight : boolean;

// Update is called once per frame
function Update () {

   
   
        // Get the input vector from keyboard or analog stick
        var directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
   
  

        //if no keyboard input... use mobile controls
        if(directionVector == Vector3.zero){

           
            //check for moved or stationary finger
            if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved || Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Stationary) {
               
                //check for change in direction every frame
                var touchDeltaPosition : Vector2 = Input.GetTouch(0).deltaPosition;
               
                //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
                if(touchDeltaPosition.x > sensitivity){

                    //set movement right
                    directionVector = Vector3.right;

                    //allows for movement after finger stops moving
                    mobileRight = true;
                }

                //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
                else if(touchDeltaPosition.x < -sensitivity){
                   
                    //set direction to left
                    directionVector = Vector3.left;

                    // set mobileRight to false allowing later testing
                    mobileRight = false;
                }

                //if touch direction is 0 (Finger NOT moving) 
                else if(touchDeltaPosition.x == 0){

                    //check to see if last direction was right,... if so move right while finger direction is 0
                    if(mobileRight){
                        directionVector = Vector3.right;
                    }

                    //if last direction was left move left as above
                    else{
                        directionVector = Vector3.left;
                    }
                }
            }
                   
        }
       
           
       
        //code for other part of game... allows me to disable movement quickly
        if(!movement){
            directionVector = Vector3.zero;
       
        }
   


        if (directionVector != Vector3.zero) {
            // Get the length of the directon vector and then normalize it
            // Dividing by the length is cheaper than normalizing when we already have the length anyway
            var directionLength = directionVector.magnitude;
            directionVector = directionVector / directionLength;
       
            // Make sure the length is no bigger than 1
            directionLength = Mathf.Min(1, directionLength);
       
            // Make the input vector more sensitive towards the extremes and less sensitive in the middle
            // This makes it easier to control slow speeds when using analog sticks
            directionLength = directionLength * directionLength;
       
            // Multiply the normalized direction vector by the modified length
            directionVector = directionVector * directionLength;
        }
   
        // Apply the direction to the CharacterMotor
           motor.inputMoveDirection = transform.rotation * directionVector;
       
        //accepts mobile tap of first or second touch, space, or click right and left to make character jump
            if(movement && Input.GetKeyDown (KeyCode.Space) || movement && Input.GetMouseButtonDown(0) || movement && Input.GetMouseButtonDown(1)){
            motor.inputJump = true;
            //motor.inputJump = Input.GetButton("Jump");
        }
        else{
            motor.inputJump = false;
        }
   
}

// Require a character controller to be attached to the same game object
@script RequireComponent (CharacterMotor)
@script AddComponentMenu ("Character/FPS Input Controller")
4 Likes

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.