2
\$\begingroup\$

I'm creating a tower defence game and I've been struggling on one of the core parts of the game. What I'm trying to do is create a plane that the user can clink on and build a tower at that location. Although I've searched for a while I could not find an tutorials or useful code on this subject.

The theory is simple. I started with making a script that creates small cubes to shape the platform the game will be played on.

public Transform PlatPrefab;
public Vector3 Size;
public int platX, platY, playZ;

// Use this for initialization
void Start () {
    MakeGrid();
}

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

        for (int x = 0; x < Size.x; x++) {
                for (int z = 0; z< Size.z; z++) {

                Instantiate(PlatPrefab, new Vector3(x+platX,platY,z+playZ), Quaternion.identity);
        }
    }
}

This is the part I'm stuck on, how do I develop a system that gets the coordinates of the single cube clicked on? I've worked with Raycast Hit and I'm only comfortable with it by simply using tags and if the mouse clicks on an object with the tag then do something. However I can't wrap my head around how I'm supposed to do this with Grids. Won't they need to be labeled for my to pick one individually?

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

I have done this before in a quick and dirty way - using colliders. Put a collider and a script with an OnMouseDown method on the prefab, the method will trigger when the tile is clicked

\$\endgroup\$
0
\$\begingroup\$

This is untested, but it should get you on the right path. You can replace "Platform" with whatever layer you end up putting your platform cubes in.

if (Input.GetMouseButtonDown(0)) {
   Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
   RaycastHit hit;
   // An arbitrary distance value. Increase or decrease this value, or you could set to Mathf.Infinity.
   float distance = 100;
   // Only perform these raycasts against this particular layer.
   int platformLayer = LayerMask.NameToLayer("Platforms");
   if (Physics.Raycast(ray, out hit, distance, 1 << platformLayer)) {
      // The ray hit something! hit will have lots of information about what was hit. 
      Debug.Log("Platform at position " + hit.transform.position);
   }
}

This should get you the position of the particular platform cube that was hit. Of course, there are other ways to perform raycasts, but this is one example.

Note that in order for the Raycast to detect something, your objects need to have a collider attached.

\$\endgroup\$
  • \$\begingroup\$ Thank you for responding. However I don't understand the use of layers. I placed this in update, created a layer named "Platforms" and ensured my objects had colliders on them. I even debugged to see if Raycast works and it does. The code stops at the 2nd if statement. Could you explain to me what the line does? \$\endgroup\$ – Ozan Apr 8 '14 at 19:30
  • \$\begingroup\$ You have to make sure they are added to the Platforms layer, since we are telling Physics.Raycast to only return true if we hit that layer. Simply make sure that particular prefab is added to the Platforms layer in the Editor (see docs.unity3d.com/Documentation/Components/Layers.html for more info). Side note: 1 << platformLayer performs a bitshift on that layer index to ensure that that particular layer isn't ignored in the raycast (see docs.unity3d.com/Documentation/ScriptReference/… for more info on Raycasts in general). \$\endgroup\$ – Cooper Apr 8 '14 at 19:40
  • \$\begingroup\$ The cubes are all in the layer "Platforms" I've checked this numerous times. I seem to be triggering "if (Physics.Raycast(ray..." Regardless of where I click \$\endgroup\$ – Ozan Apr 8 '14 at 20:22
  • \$\begingroup\$ So do you get the Debug.Log statement to appear, or not? If not, and you're sure the prefabs are on the Platforms layer, you may need to increase the distance value. \$\endgroup\$ – Cooper Apr 8 '14 at 22:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.