Snap to grid is bugged; Rick's fix only causes units to interact with many lanes

The hot fix that has us extend the collider does not work for me because the colliders in other lanes are starting to touch and causing problems like the attacker above a defender attacking them, here are some pictures I show my code in it as well but here is my code pasted out, let me know what else you need!


Here I show how it currently snaps:
This shows that they are still overlapping, and the box collider is already pushing into the next row, yet I can still do this: ![Screenshot (14)|690x388] (upload://7JpodJr1bofdKHCqGYzMnJnYAcr.jpeg) It may be bad quality, but the box collider on the mole is shown:

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

public class SpawnDefenders : MonoBehaviour
{
    Defender defender;
    private void OnMouseDown()
    {
        AttemptToPlaceDefender(GetSquareClicked());
    }
    private Vector2 GetSquareClicked()
    {
        Vector2 clickPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
        Vector2 worldPos = Camera.main.ScreenToWorldPoint(clickPos);
        Vector2 gridPos = SnapToGrid(worldPos);
        return gridPos;
    }


    public void SetSelecDefender(Defender defender2Select)
    {
        defender = defender2Select;
    }
    private void AttemptToPlaceDefender(Vector2 gridPos)
    {
        var MushDisplay = FindObjectOfType<MushDisplay>();
        int defenderCost = defender.MushCost();
        //if we have enough money, then spawn defender and remove munny
        if (MushDisplay.HasMush(defenderCost))
        {
            SpawnDefender(gridPos);
            MushDisplay.MinMush(defenderCost);
        }
    }
    private Vector2 SnapToGrid(Vector2 rawWorldPos)
    {
        float newx = Mathf.RoundToInt(rawWorldPos.x);
        float newy = Mathf.RoundToInt(rawWorldPos.y) ;
        return new Vector2(newx, newy);
    }
   private void SpawnDefender(Vector2 roundedPos)
    {
        
        Defender newDefender = Instantiate(defender, roundedPos, Quaternion.identity) as Defender;
        Debug.Log(roundedPos);
    }
}
Edit: Spelling and some context to screenshots

Fixed, see my post below for how I finally got it working after someone pointed out my error!

Hi Andrew,

As it seems, the background does not line up with the grid in the background. Furthermore, check where the other game objects get spawned. Their colliders must not overlap the tile. If necessary, also check the root game object (Spawers). It must be set to (0, 0, 0).

If nothing helps, please rewatch lecture “Set Up Background Playspace” and the following, and compare your settings to Rick’s. Maybe there is a difference somewhere.

1 Like

Thank you for your response! this has helped somewhat, I had moved the game object’s transform and the body, so I have fixed that, but I still have the problem with clicking and overlap.
In this screenshot, you can see Unity’s grid and my collider, I moved my back ground in order to try and show where my collider is compared to the unity grid:


When I Click here (I did not move my mouse after clicking) Very much into the next grid line; it still spawns below.
When I looked at the game object after resetting it’s transform, it looked like it was meeting at a cross section:
so I tried moving it so the plus made with the boxes was center and this happened:

I went back and reset all transforms on the defenders and not it works! turns out the crosses are the units, not the squares! i feel so silly, thank you for your help, I would have never though to edit my defender’s positions. I am so used to the square being the unit and not where they cross. is that a setting? or something specific in unity?

That looks much better. Well done. :slight_smile:

Well, maybe it helps to regard World Units like the real world unit metre (or mile or whatever you prefer). If there is a square with a size of 1 x 1 metre, is the centre automatically at (1, 1)?

While 1 of the grid tiles has got a size of 1 x 1 WU, the coordinates don’t refer to a tile but to an origin point, which is defined as (0, 0, 0). In Unity, that origin point is located on a cross. That’s why the centre of the a tile is at (x.5, y.5), where x and y are just rounded coordinates.

:sweat_smile:

Thank you, I think I have been using pixel art programs too much

If there were half pixels in the pixel art programs, the result would probably be the same as in Unity. How the result looks also depends on where the pivot point of the pixel (or sprite) is.

you are right, Thanks again for pointing out my error!

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

Privacy & Terms