Link to home
Start Free TrialLog in
Avatar of James Hancock
James HancockFlag for United States of America

asked on

Unity c# games - is ray casting for touch detection better on images than text? How is text touch best done?

Hi
My Unity 2D word phone game is doing ray-casts to detect touches on single character text-objects.
It works perfectly for all my rectangular sprite gameObjects, - correct touch detection to the pixel.
but not well for my UI-textMeshPro objects in a 2D game. How do I make it as solid as sprite touching?
Must I add a special boundary declaration component to TMP text?
Does ray-casting work with text?

Below is my working script, that changes an image of a touched sprite...
Pretty simple. .

OnClickUp.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class OnClickUp : MonoBehaviour
{



    private SpriteRenderer spriteRenderer;

    public Sprite sprite1;
    public Sprite sprite2;
    public Sprite[] sprites;

    public void Awake() {

        spriteRenderer = GetComponent<SpriteRenderer>(); 

        spriteRenderer.sprite = sprite1;

    }


    public OnClickUp()
    {
        print("OnClickUp ctr");

    }
     
        


   
    // Start is called before the first frame update
    int currentIcon = 0;
    public void OnMouseUp()
    {



        
        print(" OnClickUp OnMouseUp");
        print("Manager HP = " + Manager.HP);
    }

    public void OnMouseDown()
    {
        print(" OnClickUp OnMouseDown");
        currentIcon++;
        if (currentIcon > 1)
            currentIcon = 0;

        if (currentIcon == 0)
            spriteRenderer.sprite = sprite2;
            else
            spriteRenderer.sprite = sprite1;


        Manager.HP++;
    }

}

Open in new window


but, when it comes to UI TextMeshPro GameObjects, touch is not pinpoint accurate. It seems that any touch sometimes triggers the code.
How is textMeshPro equivalent touch listening best done? Is there a SpriteRenderer / TextRenderer class equivalent for TMP?

Thanks



Avatar of James Hancock
James Hancock
Flag of United States of America image

ASKER

This unity TMP page looks correct

Move down to there FindIntersectingWord

section..

Is this the way? Any worries? here
ASKER CERTIFIED SOLUTION
Avatar of James Hancock
James Hancock
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial