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



Game ProgrammingC#* Unity

Avatar of undefined
Last Comment
James Hancock

8/22/2022 - Mon
ASKER
James Hancock

This unity TMP page looks correct

Move down to there FindIntersectingWord

section..

Is this the way? Any worries? here
ASKER CERTIFIED SOLUTION
James Hancock

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes