How to Detect if I have touched the correct alphabet

Recommended Posts

I am working on a game in which we control a rectangular box at the bottom of the screen. Three sphere which has alphabets in it fall down. When the game starts, a word is generated from the predefined list of words(which I'll give) and we are supposed to touch the correct sphere having the alphabet based on that word. The question is how to detect if I have touched the correct sphere. 

secondly, if I have touched a correct sphere before and there is no recurrence of that alphabet in that word then during the second wave the game should not proceed if I touch the same alphabet again.

Looking forward to your answers, i have to submit this project in a couple of days. please help! (Working on Unity 3D)

Thanks

Share this post


Link to post
Share on other sites
1 hour ago, sidbhati32 said:

The question is how to detect if I have touched the correct sphere. 

I don't understand the problem. This will be a simple check to see if the player touched the correct sphere.

It would go something like this: Make a sphere. Give it a collision bounds. Use Unity's event system to see if there was a interaction with the sphere. If the sphere is the correct one execute script.

This is the exact same thing as making a button.

Edited by Scouting Ninja

Share this post


Link to post
Share on other sites

@Scouting Ninja 

Yeah collision is not a problem. 

Suppose I have got a word "Random" and three spheres having different alphabets must fall down. At least one them should be from the word "Random". How am I supposed to link the word generated with the the alphabets falling down? 

Suppose "A","S","P" fall down and I interact with A then how would Unity know that A is a correct alphabet? 

Share this post


Link to post
Share on other sites

You program the logic to tell it that A is the correct letter.  I'm not seeing the problem here?   You know what letters have been dropped and selected, just keep building the word up...

Share this post


Link to post
Share on other sites
4 hours ago, sidbhati32 said:

Suppose I have got a word "Random" and three spheres having different alphabets must fall down.

So you can check strings like any value for example:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WordCheck : MonoBehaviour {
	
    //This is our CompareWords function, for use later.
	//We make all the characters lower case so we can compare them
	void CompareWords(string InWordA, string InWordB){
		if (InWordA.ToLower () == InWordB.ToLower ()) {
			print ("Words are the same");
		} else {
			print ("Words are NOT the same");
		}
	}

	// Use this for initialization
	void Start () {
        //It's easy to see if a word is the same as a other word
		if ("Word" == "Word") {
			print ("This will always return true");
		}
		//The problem is that A is not the same as a
		if ("Word" == "word") {
			print ("This will always return false");//It's unreachable as in it will never happen
		}
		
        //This is where we use our CompareWords function
		//The function makes A->a so we can see if A(a) = a
		CompareWords ("SameWorD", "SAMEword");//Words are the same
    	  //(sameword == sameword) = true
		CompareWords ("SameWorD", "OtherWord");//Words are NOT the same
          //(sameword == otherword) = false

		//A array of strings can hold many words
		string[] MyWordArray; // The [] makes it a array
		MyWordArray = new string[5];//This is how we tell the array how large it should be

		MyWordArray [0] = "FirstWord"; //Arrays start at 0
		MyWordArray [1] = "SecondWord";
		MyWordArray [2] = "AWord";
		MyWordArray [3] = "BLAHblah";
		MyWordArray [4] = "What";//If you started at 1 it would be 5 then this would give an error

		int MyRandomNumber = Random.Range (0, 4); //0-4 is 5 digits 0 1 2 3 4
		//Arrays count from 0 upwords so in our array 0 = "FirstWord"

        //Here we can use the CompareWords function to check the word in the array against a other word.
		CompareWords("FirstWord",MyWordArray[0]);//"Words are the same"

		//We use the random number to select our word
		CompareWords("FirstWord",MyWordArray[MyRandomNumber]);// if MyRandomNumber = 0 it will return "Words are the same"
	}

}

This code should work as a reference. You need to learn how to use arrays and strings for your game.

 

This is very simple and basic programming. It's easy to learn, just play around with it, you won't break anything. You can just copy and paste it again.

Code is like building blocks. We see what we want and then build it from what we have.

Share this post


Link to post
Share on other sites
14 hours ago, sidbhati32 said:

@Scouting Ninja 

Yeah collision is not a problem. 

Suppose I have got a word "Random" and three spheres having different alphabets must fall down. At least one them should be from the word "Random". How am I supposed to link the word generated with the the alphabets falling down? 

Suppose "A","S","P" fall down and I interact with A then how would Unity know that A is a correct alphabet? 

You can have a array or list or whatever you use, to store the correct word. Then use an integer (char is enough) as an offset to select every alphabet in the word. say you store the word as char[] word, then you can get each char as word[offset]

and for each time you "catch" an alphabet, you check the alphabet you caught to the word[offset] then if it returns true then you know you got the right word.

 

Or, if you define the falling alphabet as a Class . Then you can add a boolean in the class like this

Class dropchar{

char containedChar c

boolean correct b

vector2f position

..... //other functions needed

}

and because your program should know what word it is dropping, it can set the boolean when it creates that object. And you just check the boolean to get if you got the right one.

Share this post


Link to post
Share on other sites

@Scouting Ninja  @Cold.bo @Mike2343

I have progressed a bit with the project - 

I have randomly generated spheres having different alphabets and predefined words. 

Presently, I cannot compare the alphabet in the sphere with the alphabets of the word. 

Let me show you the code 

 

For randomly generating different alphabets -

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GenerateRandom : MonoBehaviour {

    private Text[] Circle;

    // Use this for initialization
    void Start () {

        Circle = GameObject.FindGameObjectWithTag ("Text").GetComponentsInChildren<Text> ();
        char[] S = "qwertyuiopasdfghjklzxcvbnm".ToCharArray ();


        for (int i = 0; i < 3; i++) {
            Circle .text = S [Random.Range (0, 25)].ToString ();


        }
    }    
    // Update is called once per frame
    void Update () {
        
    }
}
 

 

 

For Randomly generating a word -

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class RandomWord : MonoBehaviour {

    string[] Words = { "great", "stage", "peak", "street", "please" };

    private Text texter; 
    // Use this for initialization
    void Start () {
        texter = GameObject.FindGameObjectWithTag ("Sphere").GetComponent<Text>();
        texter.text =  Words[Random.Range (0, 4)].ToString();
    }
    
    // Update is called once per frame
    void Update () {
        
    }
}
 

 

For collision detection and checking the correct alphabet - 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Destroy : MonoBehaviour {


    private char[] Check;
    private string Me,You;

    // Use this for initialization
    void Start () {

        Me = GameObject.FindGameObjectWithTag ("Sphere").GetComponent<Text> ().ToString ();


    }
    
    // Update is called once per frame
    void Update () {
        

    }

    void OnCollisionEnter2D(Collision2D other)
    {
        
        int Length = Me.Length;

        Check = Me.ToCharArray ();

        Debug.Log ("Collision");

        if(GameObject.FindWithTag ("Background"))
        {
            You = other.gameObject.GetComponent<Text> ().ToString ();
            if(You != null)
            {
                for(int i=0 ; i<Length; i++)
                {
                    if (You == Check .ToString ())
                        Debug.Log ("Matched");

                }
            }
        }

        Destroy (other.gameObject);

}
}

 

 

Now I am getting the issue in the picture I have shared.

 

When I double click the error, it redirects me to -

     You = other.gameObject.GetComponent<Text> ().ToString ();

 

Looking forward to your help :) 

Thanks 

Untitled.png

Share this post


Link to post
Share on other sites
9 hours ago, sidbhati32 said:

Presently, I cannot compare the alphabet in the sphere with the alphabets of the word. 

There is no link between them.

I will make a example to show you, but at the moment I have a strict deadline and have to finish that first.

Share this post


Link to post
Share on other sites
9 hours ago, sidbhati32 said:

If you can link me to any tutorial that will help me in my case, it would be a great help.

Use google and search "Unity ray trace tutorials" and "Unity communicating between scripts."

 

Edited by Scouting Ninja

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now