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

Touch Thumbstick Maths

Discussion in 'Scripting' started by CatchFires, Jan 20, 2021.

  1. CatchFires

    CatchFires

    Joined:
    Jul 19, 2020
    Posts:
    38
    I made this touch thumbstick script, my maths is pretty subpar so it took me a while, everything seems to be working except for one issue, when I release the thumbstick and it's axis is supposed to be 0, 0 the x axis becomes some weird number with letters but the y goes back to 0.

    Why?

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.EventSystems;
    4.  
    5. public class ThumbStick : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
    6. {
    7.  
    8.     [SerializeField]
    9.     private Vector2 originalStickPosition;
    10.  
    11.     private Vector2 initialContactPoint;
    12.  
    13.     public Vector2 Axis;
    14.  
    15.     private float radius;
    16.  
    17.  
    18.     void Start()
    19.     {
    20.         originalStickPosition = this.transform.position;
    21.  
    22.         radius = this.gameObject.GetComponentInParent<Image>().sprite.rect.width * 2.5f;
    23.  
    24.     }
    25.  
    26.  
    27.     void Update()
    28.     {
    29.  
    30.         Axis = (-originalStickPosition + (Vector2)this.transform.position) / radius;
    31.  
    32.     }
    33.  
    34.  
    35.     public void OnDrag(PointerEventData eventData)
    36.     {
    37.         Vector2 realPosition = eventData.position - initialContactPoint;
    38.  
    39.         float distance = Vector3.Distance(realPosition, originalStickPosition);
    40.  
    41.         if (distance > radius)
    42.         {
    43.             Vector3 fromOriginToObject = realPosition - originalStickPosition;
    44.             fromOriginToObject *= radius / distance;
    45.             this.transform.position = originalStickPosition + (Vector2)fromOriginToObject;
    46.         }
    47.         else this.transform.position = realPosition;
    48.  
    49.     }
    50.  
    51.     public void OnPointerDown(PointerEventData eventData)
    52.     {
    53.    
    54.         initialContactPoint = eventData.position - originalStickPosition;
    55.     }
    56.  
    57.     public void OnPointerUp(PointerEventData eventData)
    58.     {
    59.      
    60.         this.transform.position = originalStickPosition;
    61.     }
    62.  
    63.  
    64. }
    65.  
     
  2. CatchFires

    CatchFires

    Joined:
    Jul 19, 2020
    Posts:
    38
    Got it working, this is the updated script if the answer is ever needed.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.EventSystems;
    4.  
    5. public class ThumbStick : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
    6. {
    7.  
    8.     [SerializeField]
    9.     private Vector2 originalStickPosition;
    10.  
    11.     private Vector2 initialContactPoint;
    12.  
    13.     public Vector2 Axis;
    14.  
    15.     private float radius;
    16.  
    17.  
    18.     void Start()
    19.     {
    20.         originalStickPosition = this.transform.position;
    21.         radius = this.gameObject.GetComponentInParent<Image>().sprite.rect.width * 2.5f;
    22.     }
    23.  
    24.  
    25.     void Update()
    26.     {
    27.         Vector2 moveVector = -originalStickPosition + (Vector2)this.transform.position;
    28.         moveVector = moveVector.normalized;
    29.         Axis = (moveVector);
    30.     }
    31.  
    32.  
    33.     public void OnDrag(PointerEventData eventData)
    34.     {
    35.         Vector2 realPosition = eventData.position - initialContactPoint;
    36.  
    37.         float distance = Vector3.Distance(realPosition, originalStickPosition);
    38.  
    39.         if (distance > radius)
    40.         {
    41.             Vector3 fromOriginToObject = realPosition - originalStickPosition;
    42.             fromOriginToObject *= radius / distance;
    43.             this.transform.position = originalStickPosition + (Vector2)fromOriginToObject;
    44.         }
    45.         else this.transform.position = realPosition;
    46.  
    47.     }
    48.  
    49.     public void OnPointerDown(PointerEventData eventData)
    50.     {
    51.    
    52.         initialContactPoint = eventData.position - originalStickPosition;
    53.     }
    54.  
    55.     public void OnPointerUp(PointerEventData eventData)
    56.     {
    57.        
    58.         this.transform.position = originalStickPosition;
    59.     }
    60.  
    61.  
    62. }
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756