Turret Placement in Turret Defense Game

This script is used to place turrets on a grid of tiles. Everything works well until I place one turret. When I place one Turret it doesn't let me place anymore, when I delete the turret while playing it will let me place one again. Any Idea why?

function Update () { if(Input.GetMouseButton(0)) { Debug.Log("Cast Ray");

    var hit : RaycastHit;

    var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);

        if (Physics.Raycast (ray, hit, 100.0))


            if(hit.collider.gameObject.tag == "EmptyTile")

            hit.collider.gameObject.tag = "FullTile";

            Debug.Log("Hit Empty");

            var turretPos = hit.collider.gameObject.transform.position;




more ▼

asked Apr 02, 2013 at 06:00 AM

Jarsh13 gravatar image

15 4 6 7

"doesn't let me" is pretty broad. What do your Debug.Log() statements say? Are you getting an "Hit Empty" output? If you do a Debug.Log(hit.collider.name), what is output? Is it possible the turret has an oversized collider?

Apr 02, 2013 at 06:11 AM robertbu

sorry should have clarified. i do not get a hit empty debug. I did some more tests and found that when I take away the if statement that checks the tag on the object it will let me place them but only on top of the first turret. they continue to stack no matter were I click.

Apr 02, 2013 at 07:34 PM Jarsh13

You need to figure out what you are hitting. As mentioned above, put a Debug.Log(hit.collider.name) in your script. What you are seeing would happen if you had an oversized collider on your turret.

Apr 02, 2013 at 07:45 PM robertbu

Yes I believe that's it, its really obvious now thank you. I used a sphere collider to detect targets I guess I'll use a diffrent layer for the ray.

Apr 03, 2013 at 01:05 AM Jarsh13
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

I'll just answer this myself I guess. The problem was that when I instantiated My Turret it had a large sphere collider on it that blocked the ray from hitting the tile. I just added a layer for tile collison and set the ray to ignore all other layers.

This helped me figure out how to setup a layer mask for a Ray cast.

more ▼

answered Apr 05, 2013 at 07:45 PM

Jarsh13 gravatar image

15 4 6 7

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Apr 02, 2013 at 06:00 AM

Seen: 710 times

Last Updated: Apr 05, 2013 at 07:45 PM