Incorrect Tile Selection in Isometric Z as Y tilemap

Hello guys,
I am working on a 2D isometric game and have used a isometric z as y grid for tilemap.Here i am taking giving a position to as Vector3Int for the Player to Instantiate at runtime and change the tile below him to a tile provided. However The location he is getting spawned at tile location in inspector and tile location visually are all different. In the same i am also trying to provide a goal tile/cell for the player to move to. but the tile that i am selecting through mouse is not getting selected and instead the tile with a bit of offset is getting selected.

Here in the Image pink tile is starting tile which should be under the player.
Black tile in white circle1 is the location given by grid as selected in response to clicking my mouse at location white circle2.

This is the code:

if (isFinished == true)
{
StartPos = GameObject.Find(“PlayerHolder”).GetComponent().CellPosition;
AstarDebugger.MyInstance.CreateTiles_Start(StartPos);
isFinished = false;
}
if(Input.GetMouseButtonDown(0))
{
RaycastHit2D hit = Physics2D.Raycast(camera.ScreenToWorldPoint(Input.mousePosition),Vector2.zero,Mathf.Infinity,mask);
if(hit.collider!=null)
{
Vector2 MousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3Int clickPos = grid.WorldToCell(MousePos);
GoalPos = clickPos;
AstarDebugger.MyInstance.CreateTiles_Goal(clickPos);
//ChangeTile(clickPos);
}
}

Thanks For your Help

Is the grid properly set at (0,0,0)? I haven’t used WorldToCell so I’m not sure what it expects.

Yes the grid is properly set at origin

Time to track down the data!

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

Also, for future reference, if you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

Thanks a lot for the advice
On the same note I logged some values and found out somethings:

  1. As you see in the image the first log is the mouse position where I clicked and second log is the world to cell conversion as per unity.
    2)But If we click on the tile(purple) and see in inspector we see that the location it is visually spawning is(-6,-3,0) that is not the same as it has converted i.e(-7,-3,0) .
    3)So if I go and check in the location (-7,-3,0) I can see the purple Tile in the inspector but visually there is grass tile at this location.
    So I think so there is some kind of visual interpretation problem. Where we are specifying the value is not the location it is showing at(visually) but in system it is present at that location