How do I add a forward and backward move by a swipe to my iOS script?

Hey, everyone,

I almost have it all! With help from you guys, I have my left and right swipe script working great but I want to move forward and backward on the up and down swipe instead of rotating. I don’t need my players looking at the ceiling or the floor. This is what I have so far:

#pragma strict

private var h : float;
private var v : float;
var horozontalSpeed : float = 1.0;
var verticalSpeed : float = 1.0;

function Update()
{
    if (Input.touchCount == 1)
    {
        var touch : Touch = Input.GetTouch(0);

        if (touch.phase == TouchPhase.Moved)
        {
            h = horozontalSpeed * touch.deltaPosition.x ;
            transform.Rotate( 0, -h, 0, Space.World );

            v = verticalSpeed * touch.deltaPosition.y ;
            transform.Rotate( v, 0, 0, Space.World );
        }
    }
}

I want to change the “v = verticalSpeed * touch.deltaPosition.x; transform.Rotate( v, 0, 0, Space.World );” line from rotating the player to moving him forward and backward. So, when you swipe left and right, the player turns left and right but when you swipe up and down, the player moves forward and backward. I thought it was a deltaPosition.z but that didn’t work for me when I tried it. I looked around but all I can find is information about rotation and not forward and backward movement.

Any help fixing my javascript would be greatly appreciated.

Thanks so much!
Tom

Hi again, you have an input already for the Y-slide ( the variable v ), it is just how you use it. Movement is just a change in the transform.position. Replace the last line ( after v is calculated ) with :

transform.position += transform.forward * v * Time.deltaTime;

Edit : here is my test script :

#pragma strict

public var horizontalSpeed : float = 1.0;
public var verticalSpeed : float = 1.0;

private var h : float = 0.0;
private var v : float = 0.0;

private var lastPos : Vector3 = Vector3.zero;

function Update()
{
#if UNITY_EDITOR
    if ( Input.GetMouseButtonDown(0) )
    {
    	lastPos = Input.mousePosition;
    }
    else if ( Input.GetMouseButton(0) )
    {
	    var delta = Input.mousePosition - lastPos;
	
            h = horizontalSpeed * delta.x ;
            transform.Rotate( 0, -h, 0, Space.World );

            v = verticalSpeed * delta.y ;
            transform.position += transform.forward * v * Time.deltaTime;
	    
	    lastPos = Input.mousePosition;
    }
#else
    if (Input.touchCount == 1)
    {
        var touch : Touch = Input.GetTouch(0);

        if (touch.phase == TouchPhase.Moved)
        {
            h = horizontalSpeed * touch.deltaPosition.x ;
            transform.Rotate( 0, -h, 0, Space.World );

            v = verticalSpeed * touch.deltaPosition.y ;
            transform.position += transform.forward * v * Time.deltaTime;
        }
    }
#endif
}