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

Independent controls

Discussion in 'Scripting' started by geeksband, Aug 20, 2017.

  1. geeksband

    geeksband

    Joined:
    Aug 14, 2014
    Posts:
    16
    Good morning. I want to make the control of the object, regardless of where you press (ie I'll move to the right and the object moves to the right, etc.). Now I have realized when I click on a point, the object moves there.

    I want the object to move according to the previous touch. Instead of coordinates.
    Sorry for my english.

    Now like:
    Code (CSharp):
    1. /// <summary>
    2.     /// </summary>
    3.     void MoveToAdd(float addX, float addY)
    4.     {
    5.         float x = Mathf.Lerp(cachedTransform.position.x, cachedTransform.position.x + addX, Time.deltaTime * SPEED_TOUCH);
    6.         float y = Mathf.Lerp(cachedTransform.position.y, cachedTransform.position.y + addY, Time.deltaTime * SPEED_TOUCH);
    7.         x = Mathf.Clamp(x, LEFT_BORDER, RIGHT_BORDER);
    8.         y = Mathf.Clamp(y, UP_BORDER, DOWN_BORDER);
    9.  
    10.         cachedTransform.position = new Vector3(x, y, 0);
    11.     }
    12.  
    13.     /// <summary>
    14.     /// </summary>
    15.     private void Control()
    16.     {
    17.         isControl = false;
    18.         if (controllerType == ControllerType.Swipe)
    19.         {
    20.             if (Input.touchCount > 0)
    21.             {
    22.                 Touch t = Input.touches[0];
    23.                 pos = Camera.main.ScreenToWorldPoint(t.position);
    24.  
    25.                 float x = (pos.x + previousPosition.x) >= 0 ? speed : -speed;
    26.                 float y = (pos.y + previousPosition.y) >= 0 ? speed : -speed;
    27.                 if (!isControl) MoveToAdd(x, y);
    28.  
    29.                 previousPosition = Camera.main.ScreenToWorldPoint(t.position);              
    30.             }
    31.         }
    32.         else
    33.         {
    34.             Accelometer();
    35.         }
    36.     }

    1.PNG
     
  2. geeksband

    geeksband

    Joined:
    Aug 14, 2014
    Posts:
    16
  3. geeksband

    geeksband

    Joined:
    Aug 14, 2014
    Posts:
    16
    last up
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Just remove the bit where you send it to the touch position.

    Code (CSharp):
    1. /// <summary>
    2.     /// </summary>
    3.     void MoveToAdd(float addX, float addY)
    4.     {
    5.         float x = Mathf.Lerp(cachedTransform.position.x, cachedTransform.position.x + addX, Time.deltaTime * SPEED_TOUCH);
    6.         float y = Mathf.Lerp(cachedTransform.position.y, cachedTransform.position.y + addY, Time.deltaTime * SPEED_TOUCH);
    7.         x = Mathf.Clamp(x, LEFT_BORDER, RIGHT_BORDER);
    8.         y = Mathf.Clamp(y, UP_BORDER, DOWN_BORDER);
    9.  
    10.         cachedTransform.position = new Vector3(x, y, 0);
    11.     }
    12.  
    13.     /// <summary>
    14.     /// </summary>
    15.     private void Control()
    16.     {
    17.         if (controllerType == ControllerType.Swipe)
    18.         {
    19.             if (Input.touchCount > 0)
    20.             {
    21.                 MoveToAdd(1, 0);      
    22.             }
    23.         }
    24.         else
    25.         {
    26.             Accelometer();
    27.         }
    28.     }