How can I make an infinite floor that generates based on where the player goes?
Hey! I would like to make a script with C# in Unity3D that generates the floor near you as you explore and move around. It would be made up of 1x1 planes that seamlessly connect. It would place a new one on the invisible grid if you get somewhat close, in a way that you wouldn't see it being generated (and it would only place it if there wasn't a floor there already). I tried in many ways but I don't know how I could do this. I know exactly what I want to do I just can't get it to work, so feel free to ask me anything if I wasn't clear. Thanks :)
Here's the code I've written so far; this doesn't make a full floor though, only 2 lines of the 1x1 pieces on the x and axis.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TileGen : MonoBehaviour
{
public Transform player;
public Transform floor;
int overpassz = 0;
int overpassx = 0;
int overpasszm = 0;
int overpassxm = 0;
void Update()
{
if (player.transform.position.z > overpassz && player.transform.position.x < overpassx)
{
overpassz += 10;
Instantiate(floor, new Vector3 (0, 0, overpassz), Quaternion.identity);
}
if (player.transform.position.x > overpassx && player.transform.position.z < overpassz)
{
overpassx += 10;
Instantiate(floor, new Vector3 (overpassx, 0, 0), Quaternion.identity);
}
if (player.transform.position.z < overpasszm)
{
overpasszm -= 10;
Instantiate(floor, new Vector3 (0, 0, overpasszm), Quaternion.identity);
}
if (player.transform.position.x < overpassxm)
{
overpassxm -= 10;
Instantiate(floor, new Vector3(overpassxm, 0, 0), Quaternion.identity);
}
}
}
Answer by canslp · Jul 28, 2020 at 07:46 PM
you might want to consider making a custom mesh! it's maybe the most complicated way to do it but it also gives you the most control. like make an empty floor object and give it a mesh renderer with no input, then add a script. there's lots of good tutorials for making mesh scripts, but essentially you'd want to constantly generate new triangles beneath the player when they approach a new area.
(here's the video i watched a while back that really helped) https://www.youtube.com/watch?v=eJEpeUH1EMg
Your answer
![](https://koobas.hobune.stream/wayback/20220613004014im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to automatically generate a new int with a specific name? 1 Answer
Can't get raycast to mousePosition 0 Answers
How can i make my player look at the same direction where its moving? [JOYSTICK ANDROID] 0 Answers
Navmesh path to best of several destinations 0 Answers
I need help with my scripts 0 Answers