1
\$\begingroup\$

I am making a typing game. There are words falling down what the user have to type, before they can call the OnBecameInvisible() event.

When the user types a character I want to apply a force and a torque to it and make it fall down. But to do this I have to instantiate a TextMeshPro GameObject at the position where that letter was located inside that word. But how can I get that position?

Just to make things clear:

enter image description here

\$\endgroup\$
1
\$\begingroup\$

It's late but for some peoples who dig a bit with google :

The best solution here is to create a GameObject and a Textmesh for all letters with one parent GameObject, then your register in a KeyManager all letter you spawn and everytime you press a key you look into this KeyManager to get the letter GameObject to SetParent(null) and shoot the letter away

You can spawn easily this hierarchy with a script where you give a string as a parameter

typical hierarchy for the word you spawn ("WORD" as an example)

public class KeyManager : MonoBehaviour
{
    List<GameObject> ListOfSpawnWord = new List<GameObject>();

    public void EjectLetterIfExist(string a_letter) {
        foreach (GameObject currentGameObject in ListOfSpawnWord) {
            GameObject letterToKickAway =  currentGameObject.transform.Find(a_letter+"_GO").gameObject;//get the GameObject you want kick away
            if (letterToKickAway != null)
            {
                letterToKickAway.transform.SetParent(null);
                letterToKickAway.GetComponent<Rigidbody2D>().AddForce(new Vector2(-1, 0) * 100);
            }
        }
    }
    public void AddSpawnedWord(GameObject a_gameobject)
    {
        ListOfSpawnWord.Add(a_gameobject);
    }
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .