Search Unity

creating a touch area in unity

Discussion in 'Scripting' started by Flipp3rixPablo, Jul 26, 2019.

  1. Flipp3rixPablo

    Flipp3rixPablo

    Joined:
    Jul 20, 2019
    Posts:
    4
    Hello guys, i'm developing an fps game on smartphone and got stuck on the commands. Basically what i wanted to do was to use the left area of the smartphone's screen just to look around(so camera movements), so i created a canvas as a touch area(named "ta" in the script) but it doesn't work. Without declaring the touch inputs inside the if [if (ta.GetComponent<Rect>().Contains(t.position))] camera commands works just fine but i'd like it to to work only in the left half of the screen( maybe in a canvas/panel).

    Here's the code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Mov : MonoBehaviour
    7. {
    8.     public Joystick joystickCamera;
    9.     public GameObject player;
    10.     public Camera thisC;
    11.     private Touch we = new Touch();
    12.     public float speed = 0.1f, moveSpeed;
    13.     public bool pressed = false;
    14.     Transform cameraTransform;
    15.     public GameObject ta;
    16.     private float rotX =0f;
    17.     private float rotY =0f;
    18.     public GameObject testo;
    19.  
    20.     // Start is called before the first frame update
    21.     void Start()
    22.     {
    23.         testo = GameObject.FindGameObjectWithTag("t");
    24.         ta = GameObject.FindGameObjectWithTag("bU");
    25.         cameraTransform = Camera.main.transform;
    26.         joystickCamera = Joystick.FindObjectOfType<Joystick>();
    27.         player = GameObject.FindGameObjectWithTag("Player");
    28.  
    29.         testo.GetComponent<Text>().text = "ASD";
    30.         //Screen.lockCursor = true;
    31.         //cameraTransform.eulerAngles = new Vector3(Mathf.Clamp(cameraTransform.eulerAngles.x, -90, 90), 0, 0);
    32.     }
    33.     /// </summary>
    34.     // Update is called once per frame
    35.     void Update()
    36.     {
    37.         foreach (Touch t in Input.touches) {
    38.             if (ta.GetComponent<Rect>().Contains(t.position)) {
    39.                 testo.GetComponent<Text>().text = "HELP";
    40.                 //Debug.Log("fff");
    41.             //if () {
    42.                 if (t.phase == TouchPhase.Began)
    43.                 {
    44.                     we.position = t.position;
    45.                 }
    46.                 if (t.phase == TouchPhase.Moved)
    47.                 {
    48.                     float deltaX = we.position.x - t.position.x;
    49.                     float deltaY = we.position.y - t.position.y;
    50.                     rotX -= deltaX * Time.deltaTime * speed;
    51.                     rotY += deltaY * Time.deltaTime * speed;
    52.                     //this.transform.eulerAngles = new Vector3(rotY, rotX, 0f);
    53.                     thisC.transform.eulerAngles = new Vector3(rotY, rotX, 0f);
    54.                     //player.transform.Rotate(0, thisC.transform.rotation.y, 0);
    55.                     player.transform.eulerAngles = new Vector3(0f, rotX, 0f);
    56.                 }
    57.             }
    58.         }
    59.         /*player.transform.Rotate(0, Input.GetAxis("Mouse X") * speed * Time.fixedDeltaTime,0);
    60.         this.transform.Rotate(-Input.GetAxis("Mouse Y") * speed * Time.fixedDeltaTime, 0,0);*/
    61.  
    62.         if (pressed)
    63.         {
    64.             player.transform.Translate(Vector3.forward * moveSpeed);
    65.         }
    66.     }
    67.  
    68.     public void onPointerDownRaceButton(UnityEngine.EventSystems.BaseEventData eventData)
    69.     {
    70.         pressed = true;
    71.     }
    72.     public void onPointerUpRaceButton(UnityEngine.EventSystems.BaseEventData eventData)
    73.     {
    74.         pressed = false;
    75.     }
    76. }
    77.  
    Any help is really appreciated.
     
  2. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    if (ta.GetComponent<Rect>().Contains(t.position))
    should look for a RectTransform instead of a Rect, so you could then do
    if (ta.GetComponent<RectTransform>().rect.Contains(t.position))
    .
    You will have to do a conversion between screen space and the rect's local space. You can use the RectTransform's InverseTransformPoint method I think. https://docs.unity3d.com/ScriptReference/Transform.InverseTransformPoint.html
     
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Actually, you should probably declare your "ta" variable to be of type "RectTransform" in the first place, and call GetComponent when you are initializing it rather than every time you need to reference the component.

    If Wallace is correct about the problem (and that looks right to me), then you probably also have error messages in the Unity console from the null reference exceptions you'd be getting when you try to call .Contains on the Rect component that Unity will have failed to find.
     
  4. doctorpangloss

    doctorpangloss

    Joined:
    Feb 20, 2013
    Posts:
    270
    Use the modern EventSystem to do inputs like these. For example, take a look at IDragHandler.