Search Unity

I need help fixing a bug in my game

Discussion in 'Scripting' started by vincentmacdonald, Sep 23, 2021.

  1. vincentmacdonald

    vincentmacdonald

    Joined:
    Mar 30, 2020
    Posts:
    2
    I am creating a building based game. The building mechanic is similar to that of Minecraft with snapping on-grid systems in the 3D space. The error I am coming into is that the block from 3 angles (back, left and bottom) snaps into the block not next to the block

    Videoprojech.gif

    These are all the scripts


    The Grid System :

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

    public class CustomGrid : MonoBehaviour
    {


    public float SnapSpeed;

    public GameObject target;

    public GameObject Building;

    public Vector3 truepos;
    public float GridSize;



    void LateUpdate()
    {
    truepos.x = Mathf.Floor(target.transform.position.x / GridSize) * GridSize;
    truepos.y = Mathf.Floor(target.transform.position.y / GridSize) * GridSize;
    truepos.z = Mathf.Floor(target.transform.position.z / GridSize) * GridSize;



    Building.transform.position = truepos;
    }
    }
    The mouse raycast System :

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

    public class MousePosition : MonoBehaviour
    {

    [SerializeField] private Camera MainCamera;
    [SerializeField] private LayerMask NormalLayerMask;
    [SerializeField] private GameObject Target;

    private void Update()
    {
    Ray ray = MainCamera.ScreenPointToRay(Input.mousePosition);
    if (Physics.Raycast(ray, out RaycastHit raycasthit, float.MaxValue, NormalLayerMask))
    {
    transform.position = raycasthit.point;
    }

    Target.transform.position = new Vector3(transform.position.x, transform.position.y +0.75f, transform.position.z);
    }



    }
    And finally the Place System :
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Place : MonoBehaviour
    {

    public GameObject SpawnCube;
    public GameObject Cube;
    // Start is called before the first frame update
    void Start()
    {

    }

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

    //Spawn
    if (Input.GetMouseButtonDown(0))
    {
    Instantiate(Cube);
    }

    //Rotate

    if (Input.GetKeyDown(KeyCode.R))
    {
    SpawnCube.transform.Rotate(0, 90, 0);

    }
    }
    }
    Thank you very much for your time
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Hey, please use code tags when you post code examples.
    You are likely just looking at a rounding issue here, where you snap to your grid. You seem to be rounding up only - but honestly as it is right now im not keen on looking through the code thoroughly ;)

    As a disclaimer tho:
    If you are looking to build many blocks (several hundred or more) you must apply the same optimizations as games like Minecraft do. These games are not made of blocks - they only appear like that. A chunk, for example, is actually one single object (or a few) until you edit it. It then gets reconstructured and re-rendered reactively. If the chunk was actually made of about 16*16*256 = 65536 individual objects, you couldnt properly run even a single chunk in Minecraft. There is also other optimizations, like not rendering any vertices you dont see, ie only rendering those faces of blocks next to air. These mechanics are some of the more advanced things to implement, considering necessary optimizations. This is not supposed to talk you out of it, and if you only place a few douzen blocks it doesnt matter to you, but knowing about potential problems beforehand is always a good idea
     
    Kurt-Dekker likes this.
  3. vincentmacdonald

    vincentmacdonald

    Joined:
    Mar 30, 2020
    Posts:
    2
    Thanks