Moving objects with your finger

Sorry! How to move an object with your finger on the x axis and z?

you will have a better chance someone answers if you write your question in english

OK)) finger, the screen, the movement of the object! :slight_smile:

1 Like

here’s an edit of one of my scripts. Use the values in the variables slideMagnitudeX and Y to move your object.

private var leftFingerPos : Vector2 = Vector2.zero;
private var leftFingerLastPos : Vector2 = Vector2.zero;
private var leftFingerMovedBy : Vector2 = Vector2.zero;

public var slideMagnitudeX : float = 0.0;
public var slideMagnitudeY : float = 0.0;


function Update() 
{
	if (Input.touchCount == 1) 
	{
		var touch : Touch = Input.GetTouch(0);
		
		if (touch.phase == TouchPhase.Began)
		{
			leftFingerPos = Vector2.zero;
			leftFingerLastPos = Vector2.zero;
			leftFingerMovedBy = Vector2.zero;
			
			slideMagnitudeX = 0;
			slideMagnitudeY = 0;
			
			// record start position
			leftFingerPos = touch.position;
			
		}
		
		else if (touch.phase == TouchPhase.Moved)
		{
			leftFingerMovedBy = touch.position - leftFingerPos; // or Touch.deltaPosition : Vector2 
																// The position delta since last change.
			leftFingerLastPos = leftFingerPos;
			leftFingerPos = touch.position;
			
			// slide horz
			slideMagnitudeX = leftFingerMovedBy.x / Screen.width;
			
			// slide vert
			slideMagnitudeY = leftFingerMovedBy.y / Screen.height;
			
		}
		
		else if (touch.phase == TouchPhase.Stationary)
		{
			leftFingerLastPos = leftFingerPos;
			leftFingerPos = touch.position;
			
			slideMagnitudeX = 0.0;
			slideMagnitudeY = 0.0;
		}
		
		else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
		{
			slideMagnitudeX = 0.0;
			slideMagnitudeY = 0.0;
		}
	}
}
2 Likes

Thank you! :slight_smile:

Hi I have a problem with this. Im new to coding, and when i wrote your code in c# script it has like 5 errors in one line of code

I wrote it like this:
using UnityEngine;
using System.Collections;

public class MoveScript : MonoBehaviour
private var leftFingerPos : Vector2 = Vector2.zero;
private var leftFingerLastPos : Vector2 = Vector2.zero;
private var leftFingerMovedBy : Vector2 = Vector2.zero;

public var slideMagnitudeX : float = 0.0;
public var slideMagnitudeY : float = 0.0;

function Update()

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

if (touch.phase == TouchPhase.Began)
{
leftFingerPos = Vector2.zero;
leftFingerLastPos = Vector2.zero;
leftFingerMovedBy = Vector2.zero;

slideMagnitudeX = 0;
slideMagnitudeY = 0;

// record start position
leftFingerPos = touch.position;

}

else if (touch.phase == TouchPhase.Moved)
{
leftFingerMovedBy = touch.position - leftFingerPos; // or Touch.deltaPosition : Vector2
// The position delta since last change.
leftFingerLastPos = leftFingerPos;
leftFingerPos = touch.position;

// slide horz
slideMagnitudeX = leftFingerMovedBy.x / Screen.width;

// slide vert
slideMagnitudeY = leftFingerMovedBy.y / Screen.height;

}

else if (touch.phase == TouchPhase.Stationary)
{
leftFingerLastPos = leftFingerPos;
leftFingerPos = touch.position;

slideMagnitudeX = 0.0;
slideMagnitudeY = 0.0;
}

else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
{
slideMagnitudeX = 0.0;
slideMagnitudeY = 0.0;
}
}
}

It would help if you gave us the error(s) and on which line

As stated, please give more info in future like error messages, what you have tried, etc.

That code is actually unityJavaScript (uJS), and is quite old, and from when I was starting out too. I don’t know if the script even works with the current API, but here it is converted (but untested) :

private Vector2 leftFingerPos = Vector2.zero;
private Vector2 leftFingerLastPos = Vector2.zero;
private Vector2 leftFingerMovedBy = Vector2.zero;
public float slideMagnitudeX = 0f;
public float slideMagnitudeY = 0f;
void Update()
{
    if (Input.touchCount == 1)
    {
        Touch touch = Input.GetTouch(0);
      
        if (touch.phase == TouchPhase.Began)
        {
            leftFingerPos = Vector2.zero;
            leftFingerLastPos = Vector2.zero;
            leftFingerMovedBy = Vector2.zero;
          
            slideMagnitudeX = 0f;
            slideMagnitudeY = 0f;
          
            // record start position
            leftFingerPos = touch.position;
          
        }
      
        else if (touch.phase == TouchPhase.Moved)
        {
            leftFingerMovedBy = touch.position - leftFingerPos; // or Touch.deltaPosition : Vector2
                                                                // The position delta since last change.
            leftFingerLastPos = leftFingerPos;
            leftFingerPos = touch.position;
          
            // slide horz
            slideMagnitudeX = leftFingerMovedBy.x / Screen.width;
          
            // slide vert
            slideMagnitudeY = leftFingerMovedBy.y / Screen.height;
          
        }
      
        else if (touch.phase == TouchPhase.Stationary)
        {
            leftFingerLastPos = leftFingerPos;
            leftFingerPos = touch.position;
          
            slideMagnitudeX = 0f;
            slideMagnitudeY = 0f;
        }
      
        else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
        {
            slideMagnitudeX = 0f;
            slideMagnitudeY = 0f;
        }
    }
}

And check with the current Unity Scripting Reference (API) :
Input.GetTouch
Touch.phase
TouchPhase (enum)
Touch.position
[Touch.deltaPosition
Touch](https://docs.unity3d.com/ScriptReference/Touch-deltaPosition.html)

If you come across unity JS again, these links should help with converting :
https://answers.unity.com/questions/12911/what-are-the-syntax-differences-in-c-and-javascrip.html
http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use?