0

I'm working in a game for mobile in which I can move the map with the finger. When I tap in some objects of screen it appears a UI text in scroll with information but when I touch the screen to do scroll in the text, the map placed in the background moves. How can I get the map doesn't move when I do scroll in the information text?

This is a screenshot:

enter image description here

This is the code to move the background map:

if(dragToPan){
        if(!mapping && ready){

            if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved) {

                if(Input.GetTouch(0).position.y > screenY/12){
                    Vector2 touchDeltaPosition  = Input.GetTouch(0).deltaPosition;

                    //Check if any of the tile borders has been reached
                    CheckBorders();
                    //Translate the camera
                    cam.Translate(-touchDeltaPosition.x*dragSpeed*Time.deltaTime, -touchDeltaPosition.y*dragSpeed*Time.deltaTime, 0);

                    //Clamp the camera position to avoid displaying any off the map areas
                    ClampCam();
                }
            }
        }   
    }           

And this is how I enable the scroll text:

public class SeleccionarTesoro_LIST : MonoBehaviour {

void Start()
{
    GameObject[] hitObject = GameObject.FindGameObjectsWithTag("TESOROS");
}

public void SetHitObjectToActive(GameObject hitObject)
{
    hitObject.transform.GetChild(0).GetChild(0).gameObject.SetActive (true);
    hitObject.transform.GetChild(0).GetChild(2).gameObject.SetActive (true);

}

void Update() {

    if (Input.GetMouseButtonDown (0)) 
    {
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);


            if (Physics.Raycast (ray, out hit)) {
                SetHitObjectToActive (hit.collider.gameObject);
            }

        }
}
}

UPDATE 1: I've added this lines in the first script and it works in Unity editor, but not in Android and iOS devices. How can I solve it?

if (EventSystem.current.IsPointerOverGameObject() ||
        EventSystem.current.currentSelectedGameObject != null) {
        return;
    }
Angelsm
  • 337
  • 5
  • 15
  • Do you have the code for the UI text too? Can you just disable the background dragging whenever the user is dragging on the text? – iamJP May 16 '18 at 12:10
  • @iamJP I just update my question with the code of how I enable the scroll text. How can I disable the background dragging? – Angelsm May 16 '18 at 12:20
  • 1
    Does this question help you? https://stackoverflow.com/questions/35529940/how-to-make-gameplay-ignore-clicks-on-ui-button-in-unity3d? – iamJP May 16 '18 at 13:13
  • @iamJP I've read the post and added this code to the UI panel where scroll text is, but I don't know what code I should write to works: public class BlockUIelement : MonoBehaviour, IPointerDownHandler { public void OnPointerDown(PointerEventData eventData) { //code } } – Angelsm May 16 '18 at 15:15
  • Sorry I can't be of more help. However, I would suggest to you to put all of your input handling code into one MonoBehaviour class. Maybe call it "PlayerInput". This way your code can make a single decision on what to do given any player input. Having multiple objects handling the touch screen is just asking for trouble in my opinion. – iamJP May 16 '18 at 15:30
  • @Programmer I've updated my question and it works in Unity editor, but not in Android and iOS devices. How can I solve it? – Angelsm May 23 '18 at 17:37

1 Answers1

1

If you wish to continue in this way you could try to use a bool variable isUIActive in your background mover script, which can be set to false or true from your other script and whenever your text gets active set it to true and whenever UI gets disabled set it to false. Also include a check for isUIActive before checking the input for the background move.

Something like this:

if (!isUIActive){
    if(dragToPan){
        if(!mapping && ready){
            ...

and a function to set the variable:

public void SetUIActive (bool setTo){
     isUIActive = setTo;
}

and where you need it you can call this function and set it. Note for this to work you will need a reference for this script in your other script. Let me know if this helps.

  • Thank you for your answer! How could I implement this variable in the second script? – Angelsm May 23 '18 at 16:26
  • Make a reference to your first script. So in public class SeleccionarTesoro_LIST : MonoBehaviour { ... public YourFirstScriptClassName firstScript; public void Start(){ ... firstScript= GameObject.FindWithTag("GameObjectWithFirstScript").GetComponent(); } and in the Update function where you make text active just add this line too: firstScript.SetUIActive(true); also where you make text inactive do the same just call the function with false. Let me know if this helps. – Dániel Kőcs May 28 '18 at 14:26