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. Dismiss Notice

Question Virtual Touch zone to rotate camera (android)

Discussion in 'Scripting' started by Aokkii, Nov 2, 2021.

  1. Aokkii

    Aokkii

    Joined:
    Nov 3, 2013
    Posts:
    117
    I need some help the unity virtual touch zone included in the new standard assets,

    what i want is to be able to drag the finger to rotate the camera.

    the script from the standard assets work, kinda, but the camera doesn't stop rotating when the finger is stoped and still touching the screen, it works more like an invisible joystick than a touchzone:(

    here is the script

    pd: using the new input system, all tutorials use the old input class


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.EventSystems;
    5. using UnityEngine.Events;
    6.  
    7. public class UIVirtualTouchZone : MonoBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler
    8. {
    9.  
    10.     [Header("Rect References")]
    11.     public RectTransform containerRect;
    12.     public RectTransform handleRect;
    13.  
    14.     [Header("Settings")]
    15.     public bool clampToMagnitude;
    16.     public float magnitudeMultiplier = 1f;
    17.     public bool invertXOutputValue;
    18.     public bool invertYOutputValue;
    19.  
    20.     //Stored Pointer Values
    21.     private Vector2 pointerDownPosition;
    22.     private Vector2 currentPointerPosition;
    23.  
    24.     [Header("Output")]
    25.     public UnityEvent<Vector2> touchZoneOutputEvent;
    26.  
    27.     void Start()
    28.     {
    29.         SetupHandle();
    30.     }
    31.  
    32.     private void SetupHandle()
    33.     {
    34.         if(handleRect)
    35.         {
    36.             SetObjectActiveState(handleRect.gameObject, false);
    37.         }
    38.     }
    39.  
    40.     public void OnPointerDown(PointerEventData eventData)
    41.     {
    42.  
    43.         RectTransformUtility.ScreenPointToLocalPointInRectangle(containerRect, eventData.position, eventData.pressEventCamera, out pointerDownPosition);
    44.  
    45.         if(handleRect)
    46.         {
    47.             SetObjectActiveState(handleRect.gameObject, true);
    48.             UpdateHandleRectPosition(pointerDownPosition);
    49.         }
    50.     }
    51.  
    52.     public void OnDrag(PointerEventData eventData)
    53.     {
    54.  
    55.         RectTransformUtility.ScreenPointToLocalPointInRectangle(containerRect, eventData.position, eventData.pressEventCamera, out currentPointerPosition);
    56.      
    57.         Vector2 positionDelta = GetDeltaBetweenPositions(pointerDownPosition, currentPointerPosition);
    58.  
    59.         Vector2 clampedPosition = ClampValuesToMagnitude(positionDelta);
    60.      
    61.         Vector2 outputPosition = ApplyInversionFilter(clampedPosition);
    62.  
    63.         OutputPointerEventValue(outputPosition * magnitudeMultiplier);
    64.     }
    65.  
    66.     public void OnPointerUp(PointerEventData eventData)
    67.     {
    68.         pointerDownPosition = Vector2.zero;
    69.         currentPointerPosition = Vector2.zero;
    70.  
    71.         OutputPointerEventValue(Vector2.zero);
    72.  
    73.         if(handleRect)
    74.         {
    75.             SetObjectActiveState(handleRect.gameObject, false);
    76.             UpdateHandleRectPosition(Vector2.zero);
    77.         }
    78.     }
    79.  
    80.     void OutputPointerEventValue(Vector2 pointerPosition)
    81.     {
    82.         touchZoneOutputEvent.Invoke(pointerPosition);
    83.     }
    84.  
    85.     void UpdateHandleRectPosition(Vector2 newPosition)
    86.     {
    87.         handleRect.anchoredPosition = newPosition;
    88.     }
    89.  
    90.     void SetObjectActiveState(GameObject targetObject, bool newState)
    91.     {
    92.         targetObject.SetActive(newState);
    93.     }
    94.  
    95.     Vector2 GetDeltaBetweenPositions(Vector2 firstPosition, Vector2 secondPosition)
    96.     {
    97.         return secondPosition - firstPosition;
    98.     }
    99.  
    100.     Vector2 ClampValuesToMagnitude(Vector2 position)
    101.     {
    102.         return Vector2.ClampMagnitude(position, 1);
    103.     }
    104.  
    105.     Vector2 ApplyInversionFilter(Vector2 position)
    106.     {
    107.         if(invertXOutputValue)
    108.         {
    109.             position.x = InvertValue(position.x);
    110.         }
    111.  
    112.         if(invertYOutputValue)
    113.         {
    114.             position.y = InvertValue(position.y);
    115.         }
    116.  
    117.         return position;
    118.     }
    119.  
    120.     float InvertValue(float value)
    121.     {
    122.         return -value;
    123.     }
    124.  
    125. }
    126.  


    what do i need to change to make it more like a drag to rotate.?
     
    Last edited: Nov 2, 2021
  2. Proxy_Game

    Proxy_Game

    Joined:
    Jan 17, 2019
    Posts:
    1
    How did you solve the problem?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,710
    Please don't necro-post to old threads assuming your problem is in any way related.

    Instead, set yourself up for success by starting your own post... it's FREE!

    When you post, remember nobody here can read your mind.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    Camera stuff is pretty tricky... you may wish to consider using Cinemachine from the Unity Package Manager.

    There's even a dedicated forum: https://forum.unity.com/forums/cinemachine.136/
     
  4. GerardSimpson

    GerardSimpson

    Joined:
    Jun 6, 2017
    Posts:
    8
    I'm having this exact problem as well. The documentation around how to work with this is severly lacking, If you come up with a solution please let me know