Cloned GridDebugObjects at Y position 0

Hi,

I tried rewatching the video because my GridDebugObjects are being created at a Y position of 0 which I believe is the cause of the flickering like mentioned in the video about the Selected Unit Graphic. I’m wondering how you got around this because from the code shown in the video the new game object is instantiated at the position returned by GridSystem.GetWorldPosition() which returns the vector3 with the y value set to 0. I checked my GridDebugObject prefab and its y position is set to 0.02.

Anways just hoping for some insight on a step I might have missed.

Thanks

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

public class GridSystem
{

  #region Constructors
  public GridSystem(int width, int height, float cellSize)
  {
    this.width = width;
    this.height = height;
    this.cellSize = cellSize;

    gridObjectArray = new GridObject[width, height];

    for (int x = 0; x < width; x++)
    {
      for (int z = 0; z < height; z++)
      {
        gridObjectArray[x, z] = new GridObject(this, new GridPosition(x, z));
      }
    }


  }
  #endregion

  #region Declarations
  private int width;
  private int height;
  private float cellSize;
  private GridObject[,] gridObjectArray;
  #endregion

  #region Methods
  public Vector3 GetWorldPosition(int x, int z)
  {
    return new Vector3(x, 0, z) * cellSize;
  }

  public GridPosition GetGridPosition(Vector3 worldPosition)
  {
    return new GridPosition(Mathf.RoundToInt(worldPosition.x / cellSize), Mathf.RoundToInt(worldPosition.z / cellSize));
  }

  public void CreateDebugObjects(Transform debugPrefab)
  {
    Debug.Log(debugPrefab.position.y);

    for (int x = 0; x < width; x++)
    {
      for (int z = 0; z < height; z++)
      {
        GameObject.Instantiate(debugPrefab, GetWorldPosition(x, z), Quaternion.identity);
      }
    }
  }
  #endregion

}

The prefab’s y position isn’t the one that should be at 0.02. It’s the child object that should be raised a little

My prefab is at 0
image

The child object is not
image

This should sort out your issue

3 Likes

@bixarrio has hit this one on the head. The offset needs to be in the child object of the prefab.

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

Privacy & Terms