I am creating a chess game in Unity 2D and having an issue with the moveplates. Cannot seem to get them into proper position and alignment

I started with doing everything right from the start, all functionalities are working but the positiong of the MovePlates that are supposed to highlight the possible moves are not aligning properly.

I have tried everything from code altering, Transform values trial and error, colliders and raycasting the list goes on. I even asked AI platforms multiple times but none of the solutions work.

Now I need an expert to help me solve the problem.

Transform properties for each element:
Position of chessboard: x = 8.8, y = 8.7, z = 8.7
Scale of chessboard: x = 11.5 , y = 11.5, z = 0

Position of chesspiece: x = -3.87, y = -2.59, z=0
Scale of chesspiece: x = 6 ,y = 6, z = -1

Position of moveplate: x = 0, y = 0, z = 0
Scale of moveplate: x = 1.4375, y = 1.4375

The green and black dots represent the next possible move of the chesspiece
I need to the green dots to appear in the cells and in proportion.

Code for the first file Game.cs :

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

public class Game : MonoBehaviour
{

    public GameObject ChessPiece;

    // position for each piece
    private GameObject[,] positions = new GameObject[8, 8];
    private GameObject[] playerBlack = new GameObject[16];
    private GameObject[] playerPink = new GameObject[16];

    private string currentPlayer = "pink";

    void Start()
    {

        
        playerPink = new GameObject[]
        {
             Create("pink_rook", 0, 0),
             Create("pink_knight", 1, 0),
             Create("pink_bishop", 2, 0),
             Create("pink_queen", 3, 0),
             Create("pink_king", 4, 0),
             Create("pink_bishop", 5, 0),
             Create("pink_knight", 6, 0),
             Create("pink_rook", 7, 0),
             Create("pink_pawn", 0, 1),
             Create("pink_pawn", 1, 1),
             Create("pink_pawn", 2, 1),
             Create("pink_pawn", 3, 1),
             Create("pink_pawn", 4, 1),
             Create("pink_pawn", 5, 1),
             Create("pink_pawn", 6, 1),  
             Create("pink_pawn", 7, 1),
        };

       
        playerBlack = new GameObject[]
        {
             Create("black_rook", 0, 7),
             Create("black_knight", 1, 7),
             Create("black_bishop", 2, 7),
             Create("black_queen", 3, 7),
             Create("black_king", 4, 7),
             Create("black_bishop", 5, 7),
             Create("black_knight", 6, 7),
             Create("black_rook", 7, 7),
             Create("black_pawn", 0, 6),
             Create("black_pawn", 1, 6),
             Create("black_pawn", 2, 6),
             Create("black_pawn", 3, 6),
             Create("black_pawn", 4, 6),
             Create("black_pawn", 5, 6),
             Create("black_pawn", 6, 6),
             Create("black_pawn", 7, 6),
        };

        // Position pieces on the board
        for (int i = 0; i < playerBlack.Length; i++)
        {
            SetPosition(playerBlack[i]);
            SetPosition(playerPink[i]);
        }
    }
public GameObject Create(string name, int x, int y)
    {
        GameObject obj = Instantiate(ChessPiece, new Vector3(0, 0, -1), Quaternion.identity);

        // Adjust chess piece size
        obj.transform.localScale = new Vector3(12f, 12f, 12f); // Scale down or up depending on how big you want them

        Chessman cm = obj.GetComponent<Chessman>();
        cm.name = name;
        cm.SetXBoard(x);
        cm.SetYBoard(y);
        cm.Activate();

        // Adjust the chess piece's position on the board (to prevent overlap)
        obj.transform.position = new Vector3(x * 2.5f, y * 2.5f, -1);  // Adjust the spacing between pieces

        return obj;
    }

    public void SetPosition(GameObject obj)
    {
        Chessman cm = obj.GetComponent<Chessman>();
        positions[cm.GetXBoard(), cm.GetYBoard()] = obj;
    }

    public void SetPositionEmpty(int x, int y)
    {
        positions[x, y] = null;
    }

    public GameObject GetPosition (int x, int y) 
    {
        return positions[x, y];
    }

    public bool PositionOnBoard(int x, int y)
    {
        if (x < 0 || y < 0 || x >= positions.GetLength(0) || y >= positions.GetLength(1)) return false;
        return true;
    }
}
type here

Second file, Chessman.cs:

using System.Collections.Generic;
using UnityEngine;

public class Chessman : MonoBehaviour
{
    public GameObject controller;
    public GameObject MovePlatePrefab;

    private int xBoard = -1;
    private int yBoard = -1;
    private string player;

    public Sprite black_queen, black_knight, black_bishop, black_king, black_rook, black_pawn;
    public Sprite pink_queen, pink_knight, pink_bishop, pink_king, pink_rook, pink_pawn;

    public void Activate()
    {
        controller = GameObject.FindGameObjectWithTag("GameController");
        if (!controller)
        {
            Debug.LogError("GameController not found! Ensure there is a GameObject tagged as 'GameController'.");
        }

        SetCoords();

        SpriteRenderer sr = GetComponent<SpriteRenderer>();
        switch (this.name)
        {
            case "black_queen": sr.sprite = black_queen; player = "black"; break;
            case "black_knight": sr.sprite = black_knight; player = "black"; break;
            case "black_bishop": sr.sprite = black_bishop; player = "black"; break;
            case "black_king": sr.sprite = black_king; player = "black"; break;
            case "black_rook": sr.sprite = black_rook; player = "black"; break;
            case "black_pawn": sr.sprite = black_pawn; player = "black"; break;
            case "pink_queen": sr.sprite = pink_queen; player = "pink"; break;
            case "pink_knight": sr.sprite = pink_knight; player = "pink"; break;
            case "pink_bishop": sr.sprite = pink_bishop; player = "pink"; break;
            case "pink_king": sr.sprite = pink_king; player = "pink"; break;
            case "pink_rook": sr.sprite = pink_rook; player = "pink"; break;
            case "pink_pawn": sr.sprite = pink_pawn; player = "pink"; break;
        }
    }

    public void SetCoords()
    {
        this.transform.position = new Vector3(xBoard * 0.66f, yBoard * 0.66f, -2.0f);
    }

    public int GetXBoard() => xBoard;
    public int GetYBoard() => yBoard;
    public void SetXBoard(int x) => xBoard = x;
    public void SetYBoard(int y) => yBoard = y;

    private void OnMouseUp()
    {
        DestroyMovePlates();
        InitiateMovePlates();
        Debug.Log("Chess piece clicked");
    }

    public void DestroyMovePlates()
    {
        foreach (GameObject mp in GameObject.FindGameObjectsWithTag("MovePlate"))
        {
            Destroy(mp);
        }
    }

    public void InitiateMovePlates()
    {
        switch (this.name)
        {
            case "black_queen":
            case "pink_queen":
                LineMovePlate(1, 0); LineMovePlate(0, 1); LineMovePlate(1, 1);
                LineMovePlate(-1, 0); LineMovePlate(0, -1); LineMovePlate(-1, -1);
                LineMovePlate(-1, 1); LineMovePlate(1, -1);
                break;

            case "black_knight":
            case "pink_knight": LMovePlate(); break;

            case "black_bishop":
            case "pink_bishop": LineMovePlate(1, 1); LineMovePlate(1, -1); LineMovePlate(-1, 1); LineMovePlate(-1, -1); break;

            case "black_king":
            case "pink_king": SurroundMovePlate(); break;

            case "black_rook":
            case "pink_rook": LineMovePlate(1, 0); LineMovePlate(0, 1); LineMovePlate(-1, 0); LineMovePlate(0, -1); break;

            case "black_pawn": PawnMovePlate(xBoard, yBoard - 1); break;
            case "pink_pawn": PawnMovePlate(xBoard, yBoard + 1); break;
        }
    }

    public void LineMovePlate(int xIncrement, int yIncrement)
    {
        Game sc = controller.GetComponent<Game>();
        int x = xBoard + xIncrement;
        int y = yBoard + yIncrement;

        while (sc.PositionOnBoard(x, y) && sc.GetPosition(x, y) == null)
        {
            MovePlateSpawn(x, y);
            x += xIncrement;
            y += yIncrement;
        }

        if (sc.PositionOnBoard(x, y) && sc.GetPosition(x, y).GetComponent<Chessman>().player != player)
        {
            MovePlateAttackSpawn(x, y);
        }
    }

    public void LMovePlate()
    {
        PointMovePlate(xBoard + 1, yBoard + 2);
        PointMovePlate(xBoard - 1, yBoard + 2);
        PointMovePlate(xBoard + 2, yBoard + 1);
        PointMovePlate(xBoard + 2, yBoard - 1);
        PointMovePlate(xBoard + 1, yBoard - 2);
        PointMovePlate(xBoard - 1, yBoard - 2);
        PointMovePlate(xBoard - 2, yBoard + 1);
        PointMovePlate(xBoard - 2, yBoard - 1);
    }

    public void SurroundMovePlate()
    {
        PointMovePlate(xBoard, yBoard + 1); PointMovePlate(xBoard, yBoard - 1);
        PointMovePlate(xBoard - 1, yBoard - 1); PointMovePlate(xBoard - 1, yBoard);
        PointMovePlate(xBoard - 1, yBoard + 1); PointMovePlate(xBoard + 1, yBoard - 1);
        PointMovePlate(xBoard + 1, yBoard); PointMovePlate(xBoard + 1, yBoard + 1);
    }

    public void PointMovePlate(int x, int y)
    {
        Game sc = controller.GetComponent<Game>();
        if (sc.PositionOnBoard(x, y))
        {
            GameObject cp = sc.GetPosition(x, y);
            if (cp == null) MovePlateSpawn(x, y);
            else if (cp.GetComponent<Chessman>().player != player) MovePlateAttackSpawn(x, y);
        }
    }

    public void PawnMovePlate(int x, int y)
    {
        Game sc = controller.GetComponent<Game>();
        if (sc.PositionOnBoard(x, y) && sc.GetPosition(x, y) == null)
            MovePlateSpawn(x, y);

        if (sc.PositionOnBoard(x + 1, y) && sc.GetPosition(x + 1, y)?.GetComponent<Chessman>().player != player)
            MovePlateAttackSpawn(x + 1, y);

        if (sc.PositionOnBoard(x - 1, y) && sc.GetPosition(x - 1, y)?.GetComponent<Chessman>().player != player)
            MovePlateAttackSpawn(x - 1, y);
    }

    public void MovePlateSpawn(int matrixX, int matrixY, bool isAttack = false)
    {
        float cellSize = 1.4375f; // Cell width and height
        Vector3 position = new Vector3(
            matrixX * cellSize,       // X position
            matrixY * cellSize,       // Y position
            -2.0f                     // Z position (ensure it is in front of the chessboard)
        );

        GameObject mp = Instantiate(MovePlatePrefab, position, Quaternion.identity);
       
        mp.tag = "MovePlate"; // Ensure the tag is set for MovePlate

        MovePlate mpScript = mp.GetComponent<MovePlate>();
        if (mpScript != null)
        {
            mpScript.SetReference(gameObject);
            mpScript.SetCoords(matrixX, matrixY);
            mpScript.attack = isAttack;
        }
        else
        {
            Debug.LogError("MovePlate script is missing from the prefab.");
        }
    }
    public void MovePlateAttackSpawn(int matrixX, int matrixY)
    {
        MovePlateSpawn(matrixX, matrixY, true);
    }

    private Vector3 CalculatePosition(int matrixX, int matrixY)
    {
        float cellSize = 1.4375f; // Size of one cell
        float boardOffsetX = 8.8f; // Center of the chessboard (x position)
        float boardOffsetY = 8.7f; // Center of the chessboard (y position)

        return new Vector3(boardOffsetX + (matrixX - 0.5f) * cellSize,
                            boardOffsetY + (matrixY - 0.5f) * cellSize,
                            0); // Depth for 2D
    }

}
`



Third file, MovePlate.cs:


`using UnityEngine;

public class MovePlate : MonoBehaviour
{
    public GameObject controller;  // GameController reference
    private GameObject reference = null;   // Chess piece reference

    // Board positions
    private int matrixX;
    private int matrixY;

    // false = movement, true = attacking
    public bool attack = false;

    private SpriteRenderer spriteRenderer;

    void Start()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
        if (attack)
        {
            if (spriteRenderer != null)
            {
                spriteRenderer.color = new Color(1.0f, 0.0f, 0.0f, 1.0f); // Red color for attack
            }
            else
            {
                Debug.LogError("SpriteRenderer component is missing.");
            }
        }
    }

    public void SetCoords(int matrixX, int matrixY)
    {
        this.matrixX = matrixX;
        this.matrixY = matrixY;
        // Update position to match board coordinates
        transform.position = new Vector3(matrixX * 0.66f - 2.3f, matrixY * 0.66f - 2.3f, -3.0f);
    }

    public void SetReference(GameObject gameObject)
    {
        this.reference = gameObject;
    }

    private void OnMouseUp()
    {
        if (reference != null)
        {
            Chessman piece = reference.GetComponent<Chessman>();
            if (piece != null)
            {
                piece.SetXBoard(matrixX);
                piece.SetYBoard(matrixY);
                piece.SetCoords(); // Update the position on the board
            }
            // Destroy all move plates after moving
            DestroyMovePlates();
        }
    }

    private void DestroyMovePlates()
    {
        foreach (GameObject mp in GameObject.FindGameObjectsWithTag("MovePlate"))
        {
            Destroy(mp);
        }
    }
}

I have tried all methods to alter the position and scale, be it code, unity transform values, re-coding, and changing values and implementations there, colliders, raycasters, layering, and Chatgpt and Gemini.
All of those have been able to get me only this far and no further.

I expected it to get in line with every number i changed but nothing has worked.

3

I think your problem with alignment is related to floats like in this line:
Scale of moveplate: x = 1.4375, y = 1.4375
Try playing around with float values and see that you choose fixed numbers, lets say x=4 and y=4 and fix the rest accordingly.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật