So I am instantiating a GameObject (the one with cities and trees in the image below) and trying to align it to a hex tiles on a Sphere.

So after I instantiate the gameObject, I ray-cast to get it aligned to the tile normal and tile position but its always a slight offset.

    int random = Random.Range(0, middleIceTiles.Count);
    GameObject tile = middleIceTiles[random].getTileGameObject();
    HexCity cityObj = new HexCity(city, "city-" + middleIceTiles[random].getTileName());
    GameObject cityDummy = Instantiate(cityObj.getCityObj(), tile.transform.renderer.bounds.center + new Vector3(0,0.2f,0), Quaternion.identity) as GameObject;

                        //do a raycast here
    RaycastHit hit;
    Vector3 normal = Vector3.zero;
    if (Physics.Raycast(cityDummy.transform.position, cityDummy.transform.up * (-1.0f),out hit, 25.0f))
       if (hit.collider.gameObject == tile.gameObject)
            Debug.Log(" HIT SNOW TILE CITY " +tile.name+ " TILE TEXTURE " +HexTextureInfoDict[tile.name]);
            normal = hit.normal;
   cityDummy.transform.rotation = Quaternion.FromToRotation(Vector3.up, normal);

So I am getting a random hex tile on the sphere. Then instantiating the cityObj to the tile position. Doing a raycast from the cityObj to the tile to get the tileNormal and orienting it to the tile.

So on each level load, the city gets placed on a random hex tile. But currently, the orientation as well as the position is not exact and always at some offset position from the current tile.

What am I doing wrong here? Is there any other way I can achieve this?

enter image description here

Also, one possible solution is to snap the vertices of the hex gameObject to the hex tile itself. So essentially the 6 vertices of the hex tile has to match the 6 vertices of the hex gameObject. Is this possible? I have never dealt with meshes information of gameobjects inside unity before so I am not sure if this would work?

Something I tried was this..

 Mesh TestMesh = testTile.GetComponent<MeshFilter>().mesh;
 Vector3[] vertices = TestMesh.vertices;

And then snap these vertices of the hex tile with vertices of the gameObject that I want to place on it by looping through all points but that doesn't work either..

  • \$\begingroup\$ Just a side note, that looks pretty darn awesome. But on topic, it looks like the hexes on the "planet" or whatever are curved. Are you sure they can line up exactly? \$\endgroup\$ – Entity Nov 1 '14 at 4:37
  • \$\begingroup\$ We figured out a way to fix it. We are not using a 3d model anymore. As we are using an orthographic projection we are not just using textures to represent cities. But I would still be curious to know. If we did align the hexes properly, would it work out with my current setup? And if not, is there no way it could be corrected by programming it with the mesh geometry? \$\endgroup\$ – ckzilla Nov 1 '14 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.