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

Move object where the finger is with an offset in the direction it's facing.

Discussion in 'Scripting' started by balesss, Apr 23, 2022.

  1. balesss

    balesss

    Joined:
    Dec 16, 2015
    Posts:
    2
    Hi,

    I'm making a game that the object is moved with one finger and can be rotated using two fingers.

    For making the movement I simply using
    Code (CSharp):
    1.         if (Input.touchCount > 0){
    2.             theTouch = Input.GetTouch(0);
    3.             if(theTouch.phase == TouchPhase.Moved){
    4.                 Vector2 clickPosition = Camera.main.ScreenToWorldPoint(theTouch.position);
    5.                 transform.position = clickPosition;
    6.  
    7. ...
    But, the result is not great for me, because the object stays covered by the finger, so I wish to make a little offset, só the finger stays on unit in front of the direction of object.

    I tried using
    Vector2 clickPosition2 = transform.InverseTransformPoint(clickPosition);
    and setting the
    transform.localPosition = clickPosition2 + offsett;

    but the object stays teleporting in different places on the screen (maybe because it it's making a circular reference?)

    What should i Do?
     
  2. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    488
    You probably want to implement some kind of dead zone?
    It shouldn't be too difficult. You have
    theTouch.position
    , wrap your 5th line in the following:
    if(Vector2.Distance(clickPosition, transform.position) > deadZone)
     
  3. balesss

    balesss

    Joined:
    Dec 16, 2015
    Posts:
    2
    Hey. Maybe I wasn't clear on what I was asking. On my game the character moves immediately to the point it was clicked (it is telerpot), since I'm setting the position of the character (
    Code (CSharp):
    1. transform.position
    ) to the position of the click on screen. Thats the behaviour I want. So the character moves quickly.
    But I want that this "teleport" be in behind the click point (so, let's say the character is facing up, I want he to be teleport to
    Code (CSharp):
    1. Vector2( clickPosition.x - 1, clickposition.y)
    , and if it is facing right I want it to be
    Code (CSharp):
    1. Vector2( clickPosition.x, clickposition.y - 1 )
    and so on. So the character dont stay covered by the finger.