Jump to content
  • Advertisement
sidbhati32

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
Advertisement
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

@Scouting Ninja 

Sure Sir,

I have to submit this by 21st, see if you can help me before the date else no problem. Thanks for the help :)

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

Edited by sidbhati32

Share this post


Link to post
Share on other sites

  • Advertisement
  • Advertisement
  • Popular Tags

  • Similar Content

    • By Alexander_Vovk
      Hello Guys!
      Please share your experience, where is it better to find sales manager  specialists for indie team of 6 + people(remotely)?
      Maybe someone has a good experience of cooperation with finding projects through sale managers(USA and Canada)?
      Thank you
      Best Regards
      Alex Vovk
      Co-Founder of Sixteen Squares
      Alexander_Vovk@outlook.com
       
    • By RoKabium Games
      Been a bit quiet recently, but we've been busy bug fixing and tweaking things... Now we have lots more 'Particle effects' in the game, specifically here the Flamethrower and Enemy attacks!
    • By JoshuaFraser
      Hi and thanks for reading, I have an issue with this reactive crosshair script, everything works fine until I start changing the offset. Give the script a go and you will see what I mean, when I do SetOffset(0f); it doesnt always set back to the origional state, if anyone can spot a fix I'd be super appreciative!
      using System.Collections; using System.Collections.Generic; using UnityEngine; public class ReactiveCrosshair : MonoBehaviour { [SerializeField] GameObject c_limb_prefab; private float center_offset = 0f; private float current_offset = 0f; private float max_offset = .5f; private int number_of_limbs = 4; private float limb_length = .05f; private float limb_width = .005f; private List<GameObject> c_limbs = new List<GameObject>(); public void SetupCrosshair(){ for (int i = 0; i < number_of_limbs; i++) { GameObject line_go = (GameObject)Instantiate (c_limb_prefab); line_go.transform.SetParent (this.transform); Vector3 limb_pos = new Vector3 (0f,0f,0f); //line_go.transform.position = limb_pos; line_go.transform.localPosition = limb_pos; LineRenderer line = line_go.GetComponent<LineRenderer>(); line.startWidth = limb_width; line.positionCount = 2; line.SetPosition (0, line_go.transform.localPosition + new Vector3(center_offset, 0f, 0f)); line.SetPosition (1, line_go.transform.localPosition + new Vector3(center_offset + limb_length, 0f, 0f)); line.useWorldSpace = false; c_limbs.Add(line_go.gameObject); } if (c_limbs != null) { OrientLimbs (); SetOffset (0f); } } public void OrientLimbs(){ for (int i = 0; i < c_limbs.Count; i++) { float rotation_step = 360f / (float)c_limbs.Count; c_limbs [i].transform.RotateAround (c_limbs[i].transform.position, c_limbs[i].transform.forward, 90f + (rotation_step * (float)i)); } } public void SetOffset(float _current_spread){ float offset = Mathf.Lerp (0f, max_offset, _current_spread); for (int i = 0; i < number_of_limbs; i++) { if (offset > current_offset) { Vector3 pos = c_limbs [i].transform.position + (c_limbs [i].transform.TransformDirection (Vector3.right) * offset); c_limbs [i].transform.position = pos; } if (offset < current_offset) { Vector3 pos = c_limbs [i].transform.position - (c_limbs [i].transform.TransformDirection (Vector3.right) * offset); c_limbs [i].transform.position = pos; } } Debug.Log ("SetOffset() offset: " + offset.ToString () + " _current_spread: " + _current_spread.ToString() + " localPos: " + c_limbs[1].transform.localPosition); current_offset = offset; } }  
    • By Erik Nivala
      So, as the title says i am trying to figure out a good way sync all that information with other players in Unity. My problem is that i can't come up with a good solution since i am used to creating classes for everything e.g. attachments are its own class and then the weapon would save a reference to that attachment. But since you can't send custom classes over [Command] & [ClientRPC] i am a little stuck. A solution for this would be giving each attachment for a slot a unique ID and then passing the ID to other player but i feel like that is very error prone if other ppl add a new attachment or the IDs get mixed up.
      Is there a "standard" way that this is usually done that i am missing?
      I am fairly new to programming so any help is appreciated!
    • By MintyLyton
      I'm looking for any team / people that need a programmer for their project. I'm looking to expand my portfolio which you can see Here. I'm more experienced with Unity but I can spend the time to learn new Engines if that's your preference. I have worked on Unreal Engine 4 before but I might take some time to re-learn it, if the project requires it. Feel free to DM here or use the contact info on my website. 
    • By Woody Stevens
      Hi,
       
      I am looking for a TCP or HTTP networking library similar to Lidgren (UDP).
       
      This is primarily for sending game map data and potentially other large messages from Server to Client.
       
      I do want to keep Lidgren for my chat messages, player position, small fast updates etc. I especially love the flow of data and the library usage in general, so any libraries of a similar style would be excellent. Preferably something open source, free and reliable.
      I also must be able to swap between localhost and an ip address with ease, like Lidgren, as I run a server for singleplayer/mp/lan.
       
      My game maps are similar to minecraft, but it is 2d and only one Z-level, so i'm sending a jagged array of Tile object data (currently only enum TileID.Grass) down the pipe to the Client. Problem is if i'm sending a large map 1024 x 1024 tiles down the to client that's quite a lot of data, and Lidgren is relatively slow to build the writes (before the message is even sent!). It is fine when i'm using smaller maps < 512 x 512 ( xTiles * yTiles ).

      I know about chunking and will look into implementing this later, whilst taking into account the user's position in the world to only send nearby chunks.
       
      An example of my code that can be slow:
      private void WriteWorld(NetOutgoingMessage outgoing) { try { var world = WorldManager.Instance.CurrentWorld; outgoing.Write(world.XTiles); outgoing.Write(world.YTiles); for (int x = 0; x < world.XTiles; x++) { for (int y = 0; y < world.YTiles; y++) { // Write Tile obj data outgoing.Write((int)world.Tiles[x][y]); // <-------- Slow here when xTiles and yTiles are each > 512 ! } } } catch (Exception ex) { // log send error } }  
      I'd love to hear from you guys, especially if any of you have come across a similar challenge.
    • By ethancodes
      I'm working on a system for my game that will allow the player to stack pick ups in a queue. As one pick up expires, the next automatically activates. I'm having an issue though where if I pick up the first one, it activates fine, but if i pick up a second directly after it, it overrides the first one, activates the second one, and then once it has run it's course, everything goes back to normal gameplay, no first pick up. I'm not sure why this is happening. Hopefully someone can spot what I'm doing wrong in my code.
      Here is the code for the pick up manager:
      // Update is called once per frame void Update () { if (pickUpQueue.Count != 0 && !pickUpActive) { pickUpActive = true; pickUpQueue[0].ActivatePickUp(); } DeactivatePickUp(); } void DeactivatePickUp () { if (pickUpQueue.Count != 0 && pickUpActive) { Destroy (pickUpQueue [0]); pickUpQueue.RemoveAt (0); pickUpActive = false; } } And here is the PickUp:
      public override void ActivatePickUp () { ball.GetComponent<Ball>().Speed = 2.0f; //increase ball speed... ball.GetComponent<Ball>().StartCoroutine(timer); //...set time that power up is active }  
      There is also a Base Pick Up:
      public void OnCollisionEnter2D (Collision2D collision) { Vector2 tweak = new Vector2 (Random.Range(0f, 0.2f),Random.Range(0f, 0.2f)); this.gameObject.GetComponent<Rigidbody2D>().velocity += tweak; //if the pickup makes contact with the paddle or ball.... if (collision.gameObject.tag == "Paddle" || collision.gameObject.tag == "Ball") { GameObject.FindObjectOfType<GameManager>().GetComponent<PickUpManager>().pickUpQueue.Add(this); Destroy(gameObject); //...and finally destroy power up object } } As a side note, I am trying to find a solution to this that will work for all of my pickups. Some pickups are ammo based, some are timed. 
    • By Hellados
      Hello guys, my name is Giorgi and i'm newbie game developer i'm learning Pixel art and after pixel art  i want learn C# and don't know how and where start i'm bad with programming language and know only HTML/CSS
  • Advertisement
  • Popular Now

  • Forum Statistics

    • Total Topics
      631396
    • Total Posts
      2999789
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!