Search Unity

I need help!!!

Discussion in 'Scripting' started by M3rlinEllegant, Feb 3, 2020.

  1. M3rlinEllegant

    M3rlinEllegant

    Joined:
    Jan 20, 2020
    Posts:
    1
    Hello, I'm trying to create a game of 3D points and boxes, but I don't have much programming knowledge and here's a 2D script that I found and was trying to "adapt" to my project, but it didn't work if someone can help me I would be grateful

    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;
    using UnityEngine.SceneManagement;

    // pls use Transform instead of GameObject :: Important!!!!

    public class GameSetup : MonoBehaviour
    {




    // object
    public Transform vertical;
    public Transform horizontal;
    public Transform box;


    // Main camera
    public Camera mainCam;

    // Board Size
    public static int board_Width ;
    public static int board_Height ;

    // Board Position



    // Structure of game
    public static Transform[,] vertical_line;
    public static Transform[,] horizontal_line;
    public static Transform[,] boxs;

    public Button btn_reset;
    public Button btn_config;
    /// let user intialize the size
    ///

    void Start()
    {


    CreateBoard(board_Width,board_Height);



    }

    // Update is called once per frame
    void Update()
    {

    }

    // Create Tic tac toe board
    void CreateBoard(int board_Width, int board_Height)
    {


    float vertical_Line_Width = vertical.GetComponent<SpriteRenderer>().bounds.size.x;
    float vertical_Line_Height = vertical.GetComponent<SpriteRenderer>().bounds.size.y;

    float horizontal_Line_Width = horizontal.GetComponent<SpriteRenderer>().bounds.size.x;
    float horizontal_Line_Height = horizontal.GetComponent<SpriteRenderer>().bounds.size.y;

    float box_Width = box.GetComponent<SpriteRenderer>().bounds.size.x;
    float box_Height = box.GetComponent<SpriteRenderer>().bounds.size.y;





    //float pixel_board_width = mainCam.ScreenToWorldPoint(new Vector3((board_Width * horizontal_Line_Width + (board_Width + 1) *
    // vertical_Line_Width) / 2, 0, 0f)).x;

    // float pixel_board_height = mainCam.ScreenToWorldPoint(new Vector3(0, (board_Height * vertical_Line_Height + (board_Height + 1) *
    //horizontal_Line_Height) / 2 , 0f)).y;

    // float pix_vertical_width = vertical.GetComponent<SpriteRenderer>().sprite.rect.width;
    // float pix_vertical_height = vertical.GetComponent<SpriteRenderer>().sprite.rect.height;
    // float pix_horizontal_width = horizontal.GetComponent<SpriteRenderer>().sprite.rect.width;
    // float pix_horizontal_height = vertical.GetComponent<SpriteRenderer>().sprite.rect.height;

    //float startX = Screen.width / 2 - (board_Width * pix_horizontal_width + (board_Width + 1) * pix_vertical_width) / 2;
    // float startY = Screen.height / 2 + (board_Height * pix_vertical_height + (board_Height + 1) * pix_horizontal_height) / 2;

    float startX = Screen.width / 2;
    float startY = Screen.height / 2;
    // Debug.Log((board_Width * pix_horizontal_width + (board_Width + 1) * pix_vertical_width) / 2);


    //initial game object structure

    vertical_line = new Transform[board_Width + 1, board_Height];
    horizontal_line = new Transform[board_Width, board_Height + 1];
    boxs = new Transform[board_Width, board_Height];



    float yCor = mainCam.ScreenToWorldPoint(new Vector3(0, startY, 0f)).y;
    // middle screen
    yCor = yCor + (board_Height * vertical_Line_Height + (board_Height + 1) * horizontal_Line_Height) / 2;
    for (int y = 0; y <= board_Height; y++)
    {
    float xCor = mainCam.ScreenToWorldPoint(new Vector3(startX, 0, 0f)).x;
    //middle screen
    xCor = xCor - (board_Width * horizontal_Line_Width + (board_Width + 1) * vertical_Line_Width) / 2;
    // check.text = "x = " + xCor + " y = " + yCor;
    for (int x = 0; x <= board_Width; x++)
    {
    Vector3 center_of_Box = new Vector3(xCor, yCor, 0f);
    Vector3 center_of_Vertical = new Vector3(xCor - horizontal_Line_Width / 2 - vertical_Line_Width / 2, yCor, 0f);
    Vector3 center_of_Horizontal = new Vector3(xCor, yCor + horizontal_Line_Height / 2 + vertical_Line_Height / 2, 0f);

    if (x != board_Width && y != board_Height)
    boxs[x, y] = Instantiate(box, center_of_Box, Quaternion.identity) as Transform;

    if (y != board_Height)

    vertical_line[x, y] = Instantiate(vertical, center_of_Vertical, Quaternion.identity) as Transform;



    if (x != board_Width)
    horizontal_line[x, y] = Instantiate(horizontal, center_of_Horizontal, Quaternion.identity) as Transform;

    xCor += box_Width + vertical_Line_Width;
    }
    yCor -= box_Height + horizontal_Line_Height;
    }
    }

    public void OnClick()
    {
    Application.LoadLevel(1);
    }

    public void ChangeScene(int scene_id)
    {

    SceneManager.LoadScene(scene_id);
    }


    }
     
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    Please use code tags, so we can read the code much easier.

    Also what is the problem? Can't help you when we don't know whats happening.