Hi everyone,
I’ve recently joined the Unity community because I want to learn to build an educational game for kids and highschool students. Here’s my approach:
I want to create a game where, say, the player must identify the subject of a sentence:
Example: The apple is red
Correct answer: The apple
For this, I want to interact with the actual sentence and click on “The” and drag to “apple” in order to trigger my event “Correct!”.
Do you see this feasable? Thanks in advance for your support!
Yes this is feasible, but in my opinion not the best for a first project. However, it’s definitely doable if you’re willing to climb the learning curve. This may be beyond your current level of knowledge, but anything you have questions about I can point you to the right resource to learn about it. Otherwise use the Learn section of this website, google & youtube and you’ll figure it all out.
There are two approaches I can think of. There are advantages/disadvantages to both.
-
Create word objects that are individually interactive, and position them so that they look like a normal sentence. You would essentially convert a sentence into a bunch of word objects.
-
Using a full sentence in single object, detect where in the text the mouse is. That is the approach I’m describing below.
TextMeshPro comes with a utility class TMP_TextUtilities which has a FindNearestWord function. That is what you can use to find which word in the sentence is nearest to the current mouse position. (you must use TextMeshPro’s Text component to use that function)
Here is an example implementation:
Example.cs
using UnityEngine;
using TMPro;
public class Example : MonoBehaviour
{
public TextMeshProUGUI textMesh;
public string wordNearestToMouse;
private Camera _camera;
private int _lastWordFound = -1;
// once when the game starts
private void Start()
{
// store the camera so we don't have to keep getting it again every frame (used in Update)
_camera = Camera.main;
}
// every frame
private void Update()
{
// if there's no text component, do nothing
if(textMesh == null)
{
return;
}
// if the mouse button is being held (or a touch is being held)
if (Input.GetMouseButton(0))
{
// get the index of the word that is nearest to the mouse or touch
int nearestWordIndex = TMP_TextUtilities.FindNearestWord(textMesh, Input.mousePosition, _camera);
// if it's not the same word as before (prevent repeating work)
if (nearestWordIndex != _lastWordFound)
{
// update the last word found
_lastWordFound = nearestWordIndex;
// get the array of word information in the text
TMP_WordInfo[] wordInfo = textMesh.textInfo.wordInfo;
// get the word at that index found near the mouse
wordNearestToMouse = wordInfo[nearestWordIndex].GetWord();
Debug.Log("Word under mouse: " + wordNearestToMouse);
}
}
}
}
That code will put the nearest word in the “wordNearestToMouse” variable when the mouse button is held. I left it public so that it shows up in the inspector.
You would need to expand on that so that you create a string like “The apple” as the player clicks & drags on the word “The” and “apple”. Then your game code will need to be expecting an answer, and you compare the two. Hope that made sense.
Thank you for your reply! I’ll definitively do some research about what you’ve just told me. I must read a lot about, well, everything! but I’ll investigate your approach , sounds promising and it’s quite the thing that I was looking for.
Thanks!!!