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\$
4
  • \$\begingroup\$ Can you show what the inspector shows for the tile's location (as well as what the correct position is)? \$\endgroup\$ Aug 1, 2018 at 15:12
  • \$\begingroup\$ Added the position of the current tile and the correct position of the next tiles. \$\endgroup\$ Aug 1, 2018 at 15:20
  • \$\begingroup\$ Can someone please help me \$\endgroup\$ Aug 2, 2018 at 12:33
  • 1
    \$\begingroup\$ Begging is not constructive. If someone hasn't helped you, it's not for lack of sympathy, it's because you haven't given them enough information to know how to help you. So: any time you're tempted to beg, stop, re-read your question, or try reading it to a friend who knows nothing about the problem. Find what might be unclear to a new reader who's never seen your project, and edit it to clarify those points. Trying to reproduce your problem in a new project given only the information in the question can help you identify what's needed for an MCVE. \$\endgroup\$
    – DMGregory
    Aug 2, 2018 at 13:06

1 Answer 1

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);
}
\$\endgroup\$
9
  • \$\begingroup\$ There is still a gap between the tiles. \$\endgroup\$ Aug 3, 2018 at 10:17
  • \$\begingroup\$ Then add the difference into your nextTileOffset variable until they line up. \$\endgroup\$
    – DMGregory
    Aug 3, 2018 at 11:31
  • \$\begingroup\$ I get this error: ArgumentOutOfRangeException: Argument is out of range. Parameter name: index \$\endgroup\$ Aug 3, 2018 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, 2018 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\$ Aug 3, 2018 at 12:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .