Letters appear on the screen one by one, at a rate of one per second.
Instructions: “Letter will appear on the screen one at a time. Every time you see the letter ‘A’, click the letter with your mouse. When letters other than ‘A’ appear, do NOT do click or tap the letter”.
Scoring: Give one point if there is an error (an error is a click on a wrong letter or a failure to click on letter A).
I am completely new to Unity. Can someone help me as to how should I start building this?
Thanks.
TextMesh is a component, thus it only exists as part of a GameObject. When you create a 3D text with the Hierarchy Create button, a game object is created with a TextMesh component included.
In your case, a Raycast seems to be the more indicated: attach the script to the camera and verify what’s being clicked. The code could be something like this:
var score = 0; // the score increases each time an A is clicked
function Update(){
if (Input.GetMouseButtonDown(0)){ // if left button pressed...
// calculate a ray passing through the mouse pointer:
var ray = camera.ScreenPointToRay(Input.mousePosition);
var hit: RaycastHit;
if (Physics.Raycast(ray, hit)){ // if something hit...
var txtMsh: TextMesh = hit.transform.GetComponent(TextMesh);
// and if it's a TextMesh "A"...
if (txtMsh && txtMsh.text == "A"){
score++; // increment the score
}
}
}
}
You must add a box collider to the 3D text object - due to some weird reason, mesh colliders in 3D text objects aren’t detected by Raycast.
Notice that the logic above seems opposed to what your question says: the score is incremented when an A is clicked. If you want to increase the score when an error occurs, change the logic:
...
if (Physics.Raycast(ray, hit)){ // if something hit...
var txtMsh: TextMesh = hit.transform.GetComponent(TextMesh);
// if it's not a TextMesh, or if it's not an A...
if (!txtMsh || txtMsh.text != "A"){
score++; // increment the score
}
}
}
}