0
\$\begingroup\$

I am making a 3D game in Unity and C#. I want to spawn another tile when the player is at the end of the current tile. But when I spawn the next tile it doesn't spawn at the right place(just after the current tile). How can I fix this?

Thanks

Old Code:

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

public class Terrain : MonoBehaviour
{
    public List<GameObject> objs = new List<GameObject>();
    public GameObject terrain;
    public GameObject player;
    private float z = 4130.875f;
    private float safeZone = 260.0f;
    private float tileLength = 260.0f;
    private int animTilesCount = 2;
    void Start()
    {
        for (int i = 0; i < animTilesCount; i++)
        {
            spawnTile();
        }
    }
    void Update()
    {
        if (player.transform.position.z - safeZone >= (z - objs.Count * tileLength))
        {
            spawnTile();
            DeleteTile();
        }
    }

    public void spawnTile()
    {
        GameObject go;
        go = Instantiate(terrain) as GameObject;
        go.transform.SetParent(transform);
        go.transform.position = Vector3.forward * z;
        Debug.Log(z);
        Debug.Log(Vector3.forward * z);
        Debug.Log(go.transform.position);
        go.transform.rotation = terrain.transform.rotation;
        objs.Add(go);
        z += tileLength;
    }

    public void DeleteTile()
    {
        Destroy(objs[0]);
        objs.RemoveAt(0);
    }
}

enter image description here Current Tile That's the position of the current tile.

The correct position for the next tiles would be (6.103516e-05, 3.051758e-05, z). They have to spawn after the current tile.

\$\endgroup\$
0
\$\begingroup\$

If you just want to spawn at an offset from the previous tile, did you try reading the position of the previous tile, and adding onto that?

Just make sure your objs list is initialized in the Inspector.

public Vector3 nextTileOffset;

public void spawnTile()
{
    Vector3 position = objs[objs.Count - 1].transform.position;
    position += nextTileOffset;

    GameObject go = Instantiate<GameObject>(
                      terrain,
                      position,
                      terrain.transform.rotation );

    go.transform.SetParent(transform);

    objs.Add(go);
}
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ There is still a gap between the tiles. \$\endgroup\$ – ProgrammerProgrammerIOSAN Aug 3 '18 at 10:17
  • \$\begingroup\$ Then add the difference into your nextTileOffset variable until they line up. \$\endgroup\$ – DMGregory Aug 3 '18 at 11:31
  • \$\begingroup\$ I get this error: ArgumentOutOfRangeException: Argument is out of range. Parameter name: index \$\endgroup\$ – ProgrammerProgrammerIOSAN Aug 3 '18 at 12:09
  • \$\begingroup\$ Remember when I told you to make sure your objs list is initialized in the Inspector? In order to align with the previous tile, we need there to be at least one tile in the list when we call this function. \$\endgroup\$ – DMGregory Aug 3 '18 at 12:34
  • \$\begingroup\$ Ok but when I do this : objs.Add(terrain); then it instantly deletes terrain after launching the game. I think it's because of if (player.transform.position.z - safeZone >= (position.z - objs.Count * tileLength)) \$\endgroup\$ – ProgrammerProgrammerIOSAN Aug 3 '18 at 12:44

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.