How to Snap to Custom Grid shape

Hi nina! Sorry but my messagge was automatically deleted beacause flagged as inappropriate(?) You managed to see it at least ahahah i edited it maybe it was because i wrote “it was uc*ed up” ahahaah

1#
i already checked the pivot points are not what is causing the issue unfortunately , if you look at the console Debug i posted you can see that with the offset it place em at the right coordinates it shows there, it’s not a sprite/render problem , it just place with a -5 coordinates in x and 0.4 in y offset to begin with, maybe there something wrong in the algorithm which mess up the coordinates but i cannot grasp what it is mh

3#
Yeah I think it’s something due to the round algoritm when i click a number which is really “small”, at the top it doesn’t happen , only when it’s at the bottom

PS:i just noticed that 3# it happens also when i try to click on the bottom of the 2nd row, so it’s like i have some issues with the height of the cells, the width seems fine, also clicking at the top i don’t have any issues

also for 1# i noticed that is something to do with the worldpos-offset line, because the offset is 5.5 so i get distanciated cause of that mhh

When did you try to click the top part of the grid? After you spawned defenders in the top lane? If so, check the collider of the defender. If you followed Rick’s concept, the collider of the defender blocks the raycast of the mouse, so the raycast cannot hit the collider of the grid anymore.

Log more position values into your console. Maybe you are able to detect a pattern. If the problem is caused by the Round method, you’ll have to check the mouse position to determine on which tile you clicked. If your grid does not align with the grid in the scene, Unity’s round method won’t help anyway.

1 Like

Instantiate takes a world position. I feel that f you are not converting your grid coords to world position coord, then you’re just giving it random numbers and tweaking the results.

We should try to see what went wrong with your initial conversion attempts and see if we can fix it.

1 Like

I think you are right Anthony, i did a final video to show what is the situation with all the Debug.log set right, i’m beginning to surrender, this is my last resort ahahahah

this is still what the code working here looks like

 private void OnMouseDown()
    {
        SpawnDefender(SetPositionInGrid());

    }
   

    private void ColliderInfo()
    {
        size = collider.bounds.size; //dimensioni collider
        offset = collider.transform.position;
        Debug.Log("SIZE of collider is" + size);
        Debug.Log(" OFFSET of collider is" + offset);
        gridCellHeight = collider.size.y / gridRows;
        gridCellWidth = collider.size.x / gridColumns;
        Debug.Log("CELL Width is"+ gridCellWidth);
        Debug.Log("CELL Height is " + gridCellHeight);



    }
    private Vector2 SetPositionInGridDynamic()
    {
        Vector2 clickPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
        Debug.Log("click position is " + clickPos);
        Vector2 worldPos = Camera.main.ScreenToWorldPoint(clickPos);
        Debug.Log("click position converted in world point is " + worldPos);
        Vector2 GridCord = WorldPosToGridPos(worldPos);
        Debug.Log("final position of the defender +  offset given in editor is " + GridCord);
        Vector2 GridCellCenter = GridCord;

        return GridCellCenter;

    }

   public Vector2 WorldPosToGridPos (Vector2 worldPos)
 {
        Vector2 localPos = worldPos - offset ;
        Debug.Log("localPos (worldpos - offset) is " + localPos);
        Vector2 gridCoord;
        gridCoord.x = Mathf.Round(localPos.x / gridCellWidth) * gridCellWidth;
        Debug.Log("gridCoord.x = Mathf.Round(localPos.x / gridCellWidth) * gridCellWidth IS " + gridCoord.x);
        gridCoord.y = Mathf.Round(localPos.y / gridCellHeight) * gridCellHeight ;
        Debug.Log("  gridCoord.y = Mathf.Round(localPos.y / gridCellHeight) * gridCellHeight IS " + gridCoord.x);
        gridCoord.x = gridCoord.x + SnapXOffset;
        gridCoord.y = gridCoord.y + SnapYOffset;
        
        return gridCoord;

    }

((*PS for the onmousedown method it is so “basic” only because i implemented a drag and drop function for the placement so the “real” method is onmousedrag, but i used this to be faster in debugging, this is the reason whymaybe in the video there could be a bug where i can place more than 1 defender in a single cell, but it’s quite irrilevant i just said this to prevent questions hahah))

in the video i show everything i tried to debug , and also how “the placement when clicking at bottom bug” works, i think this will be the last, because i’m bothering you since days and i am tired too of these bugs ahahah Hoping to fix this with your last help guys

(PS. watching the video i noticed that i forgot to change the debug.log for the round of gridpos.y and it shows the X one again, sorry, but i think/hope it is not a crucial information)

I’ve just created a grid spawner. You could download my project from GitHub and test it. I think it’s relatively self-explanatory. The Grid class is supposed to hold the information for the abstract grid, while the MouseClickSpawner projects the abstract grid onto the “physical” collider (= the actual grid in your game).

1 Like

Wow thank you Nina, i’m looking at your code and it is really amazing! i feel like a little newbie ahahah
It is really self-explanatory as you said, it’s a bit different from my approach but maybe it is better because as Anthony said i think i was stranded on making mistakes and making the code messier as i went on, i waas close to a solution but at the point i was now i was just giving it random numbers and tweaking the results (as anthony said).

So maybe it’s better to clean it up by giving a clear view with what you did, thank you!
But before closing the post i would like to wait if Anthony has noticed maybe what was wrong with the
previous code in the video, and also i would like to look and implement calmly the code you wrote in my project.
thank you guys :smiley: :+1:

Don’t take my code if you have your own approach. In most cases, there are multiple ways to get something done. It is perfectly fine if you keep and fix it. I just wanted to show you what I meant in answer #18. :slight_smile:

1 Like

Yes Nina don’t worry i won’t just take drag and drop it in my project without looking into it closely, i’m here to learn :slight_smile: hahah i will try also to merge it in my logic but from what i saw the logic at the base it’s quite the same, the problem of my approach is that i half broked it ahahah so maybe it is better to start from 0 with a clean view from what you showed me hahaha

even if for now i would like to go on with the TileVania, on spare time in these days i will try to fix this little things in this project

I have just implemented the functionality to add and remove game objects to/from the tiles. If you cannot make that work with colliders, you could take a look at that solution.

1 Like

Blockquote i would like to wait if Anthony has noticed maybe

Unfortunately, I won’t be available much for the next 12 hours.

1 Like

Don’t worry as i said before i already bothered you two too much hahaaha when you have some spare time if you’d like come and take a look :slightly_smiling_face: you already did too much for me, i will go on with the course! (i only ask to not close the topic yet)

Looking at your numbers, it looks like you’ve put the GridCoord.x in the GridCoord.y log, making it hard to judge that value.

However, the GridCoord.x value for the first guy placed (I paused here at 0:23 to do some number checking) is -3.710781, which is good, because it is a multiple of your cell width, which is 1.236927. If you divide -3.710781 (the grid X coorD) by 1.236927 (cell width), you get -3. So this position you click on is on the -3 space on your x coordinate grid. The debug log is showing the wrong value for y, so we can’t check that value.

But now that we know the grid coordinate of that space on the x-axis, we can tell where on the x-axis you want the player to click. The space to the left it is obviously -4. The space to the right is -2. So I’m assuming you want to put some kind of IF check to make sure the unit is spawned only if the player clicked on a space from -4 to 2. (Or to put it another way, if the x-axis position is between -4.947708 and 2.473854). (You could round off to -5 to 3, if you want just to make sure since the next grid coordinate position will be well beyond those numbers anyway).

This will ensure that you do not spawn a character if the player clicks too far to the right or left.

You can do a similiar check with the y once you have those numbers.

image

If you can get the y-axis position of the space the guy is standing on after doing the grid-center, then you know you only want to spawn a guy if the grid-center position from the mouse click is between this number and this number - the cell height (0.8488172). If a player clicks a calculated cell higher or lower, we don’t want to spawn a guy.

One thing to note is that I think you were correct in that the formula I gave you was not calculating the grid coordinates after all, but instead calculating the world position of the cell center. So you could go ahead and use that to spawn in. That seems to be working in. The issue is that the grid we are making is infinitely large (although the collider isn’t), so you just want to make sure that only clicking on the rows and columns you want has an effect, not any others. And to figure that out, we just need to calculate the grid coordinates of a single cell (center x pos / cell width, center y pos / cell height).

It might be useful to make a function that calculates the actual grid coordinate based on world position, makes sure it’s a valid grid coordinate you want to use, and then if it is, calculates the center world position by multiplying the grid coordinates by the cell’s width and height. Right now we are combining those two functions into one by dividing and rounding (getting grid coordinates), and then immediately multiplying (converting back to world coordinates).

1 Like

Thank you Anthony ! your time in helping me when i was doing this was crucial, i’ll also use what you said to me here at last to tinker the script when i’ll get onto it again, you gave me some good insights i didn’t thought about
Thanks to Nina i understood what was wrong and i cleaned my code, as you can see above she did a github project, i’ll put it as a solution if someone will need it in the future.
Right now i am working on a platform project thanks to the Tilevania course, and it’s going on well maybe i’ll share a demo with you all when it is in a reasonable state :rofl:
You have been really helpful, thank you both :slightly_smiling_face:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms