Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Moving objects with your finger

Discussion in 'iOS and tvOS' started by Alien3D, Jul 7, 2012.

  1. Alien3D

    Alien3D

    Joined:
    Jun 22, 2012
    Posts:
    8
    Sorry! How to move an object with your finger on the x axis and z?
     
    Last edited: Jul 7, 2012
  2. Deleted User

    Deleted User

    Guest

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

    Alien3D

    Joined:
    Jun 22, 2012
    Posts:
    8
    OK)) finger, the screen, the movement of the object! :)
     
    CodeWarriorMalo likes this.
  4. AlucardJay

    AlucardJay

    Joined:
    May 28, 2012
    Posts:
    328
    here's an edit of one of my scripts. Use the values in the variables slideMagnitudeX and Y to move your object.

    Code (csharp):
    1.  
    2. private var leftFingerPos : Vector2 = Vector2.zero;
    3. private var leftFingerLastPos : Vector2 = Vector2.zero;
    4. private var leftFingerMovedBy : Vector2 = Vector2.zero;
    5.  
    6. public var slideMagnitudeX : float = 0.0;
    7. public var slideMagnitudeY : float = 0.0;
    8.  
    9.  
    10. function Update()
    11. {
    12.     if (Input.touchCount == 1)
    13.     {
    14.         var touch : Touch = Input.GetTouch(0);
    15.        
    16.         if (touch.phase == TouchPhase.Began)
    17.         {
    18.             leftFingerPos = Vector2.zero;
    19.             leftFingerLastPos = Vector2.zero;
    20.             leftFingerMovedBy = Vector2.zero;
    21.            
    22.             slideMagnitudeX = 0;
    23.             slideMagnitudeY = 0;
    24.            
    25.             // record start position
    26.             leftFingerPos = touch.position;
    27.            
    28.         }
    29.        
    30.         else if (touch.phase == TouchPhase.Moved)
    31.         {
    32.             leftFingerMovedBy = touch.position - leftFingerPos; // or Touch.deltaPosition : Vector2
    33.                                                                 // The position delta since last change.
    34.             leftFingerLastPos = leftFingerPos;
    35.             leftFingerPos = touch.position;
    36.            
    37.             // slide horz
    38.             slideMagnitudeX = leftFingerMovedBy.x / Screen.width;
    39.            
    40.             // slide vert
    41.             slideMagnitudeY = leftFingerMovedBy.y / Screen.height;
    42.            
    43.         }
    44.        
    45.         else if (touch.phase == TouchPhase.Stationary)
    46.         {
    47.             leftFingerLastPos = leftFingerPos;
    48.             leftFingerPos = touch.position;
    49.            
    50.             slideMagnitudeX = 0.0;
    51.             slideMagnitudeY = 0.0;
    52.         }
    53.        
    54.         else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
    55.         {
    56.             slideMagnitudeX = 0.0;
    57.             slideMagnitudeY = 0.0;
    58.         }
    59.     }
    60. }
    61.  
     
    Last edited: Jul 7, 2012
    youngapprentice and joaomorais like this.
  5. Alien3D

    Alien3D

    Joined:
    Jun 22, 2012
    Posts:
    8
    Thank you! :)
     
  6. CroPlay

    CroPlay

    Joined:
    Jan 1, 2018
    Posts:
    1
    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;
    }
    }
    }
     
  7. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    It would help if you gave us the error(s) and on which line
     
  8. AlucardJay

    AlucardJay

    Joined:
    May 28, 2012
    Posts:
    328
    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) :
    Code (csharp):
    1. private Vector2 leftFingerPos = Vector2.zero;
    2. private Vector2 leftFingerLastPos = Vector2.zero;
    3. private Vector2 leftFingerMovedBy = Vector2.zero;
    4. public float slideMagnitudeX = 0f;
    5. public float slideMagnitudeY = 0f;
    6. void Update()
    7. {
    8.     if (Input.touchCount == 1)
    9.     {
    10.         Touch touch = Input.GetTouch(0);
    11.      
    12.         if (touch.phase == TouchPhase.Began)
    13.         {
    14.             leftFingerPos = Vector2.zero;
    15.             leftFingerLastPos = Vector2.zero;
    16.             leftFingerMovedBy = Vector2.zero;
    17.          
    18.             slideMagnitudeX = 0f;
    19.             slideMagnitudeY = 0f;
    20.          
    21.             // record start position
    22.             leftFingerPos = touch.position;
    23.          
    24.         }
    25.      
    26.         else if (touch.phase == TouchPhase.Moved)
    27.         {
    28.             leftFingerMovedBy = touch.position - leftFingerPos; // or Touch.deltaPosition : Vector2
    29.                                                                 // The position delta since last change.
    30.             leftFingerLastPos = leftFingerPos;
    31.             leftFingerPos = touch.position;
    32.          
    33.             // slide horz
    34.             slideMagnitudeX = leftFingerMovedBy.x / Screen.width;
    35.          
    36.             // slide vert
    37.             slideMagnitudeY = leftFingerMovedBy.y / Screen.height;
    38.          
    39.         }
    40.      
    41.         else if (touch.phase == TouchPhase.Stationary)
    42.         {
    43.             leftFingerLastPos = leftFingerPos;
    44.             leftFingerPos = touch.position;
    45.          
    46.             slideMagnitudeX = 0f;
    47.             slideMagnitudeY = 0f;
    48.         }
    49.      
    50.         else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
    51.         {
    52.             slideMagnitudeX = 0f;
    53.             slideMagnitudeY = 0f;
    54.         }
    55.     }
    56. }

    And check with the current Unity Scripting Reference (API) :
    Input.GetTouch
    Touch.phase
    TouchPhase (enum)
    Touch.position
    Touch.deltaPosition
    Touch

    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?