Search Unity

How can I convert old input system script into a new input system

Discussion in 'Input System' started by qasim12345, May 26, 2021.

  1. qasim12345

    qasim12345

    Joined:
    May 26, 2021
    Posts:
    2
    I want to add fields on my touch screen so I can control my player and camera separately for that I have to use a script called "FixedTouchField.cs" but the problem is that this script was written in a unity old input system and I want to use this script with the unity new input system. So how can I make this script compatible with unity's new input system?

    here's the video explaining how to use this script with old input system: https://youtu.be/CmCVtEZUKIs

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3. public class FixedTouchField : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
    4. {
    5. [HideInInspector]
    6. public Vector2 TouchDist;
    7. [HideInInspector]
    8. public Vector2 PointerOld;
    9. [HideInInspector]
    10. protected int PointerId;
    11. [HideInInspector]
    12. public bool Pressed;
    13. // Use this for initialization
    14. void Start()
    15. {
    16. }
    17. // Update is called once per frame
    18. void Update()
    19. {
    20. if (Pressed)
    21. {
    22. if (PointerId >= 0 && PointerId < Input.touches.Length)
    23. {
    24. TouchDist = Input.touches[PointerId].position - PointerOld;
    25. PointerOld = Input.touches[PointerId].position;
    26. }
    27. else
    28. {
    29. TouchDist = new Vector2(Input.mousePosition.x, Input.mousePosition.y) - PointerOld;
    30. PointerOld = Input.mousePosition;
    31. }
    32. }
    33. else
    34. {
    35. TouchDist = new Vector2();
    36. }
    37. }
    38. public void OnPointerDown(PointerEventData eventData)
    39. {
    40. Pressed = true;
    41. PointerId = eventData.pointerId;
    42. PointerOld = eventData.position;
    43. }
    44. public void OnPointerUp(PointerEventData eventData)
    45. {
    46. Pressed = false;
    47. }
    48. }
     

    Attached Files:

  2. Memzz

    Memzz

    Joined:
    Jul 15, 2019
    Posts:
    1
    Hi
    Did you ever fix this?