- Home /
Question by
Evaldas_Unity · Nov 13, 2016 at 07:39 PM ·
instantiatedestroydistance
Creating more objects the closer you get
Hey people.
I am trying to make an 'endless' tunnel. The closer the player gets to an object with script, the more (and further placed) arcs will be spawned. After reaching midpoint, arc's behind the player would start to dissapear. So entering from the other end of this tunnel would work the same way. (pic of my idea)
I was able to create one arc using this code:
using UnityEngine;
public class instantiateArc: MonoBehaviour
{
public GameObject main;
public GameObject cube;
public GameObject player;
public float radius;
public float distance = 3f; // How far away you have to be for object to be Instantiate
private bool make = true; // Keep making objects ?
void Update()
{
Vector3 oPos = main.transform.position;
Vector3 pPos = player.transform.position;
float dist = Vector3.Distance(pPos, transform.position);
if (dist <= distance && make == true)
{
for (float i = 0.0f; i < 180.0f; i = i + 2.4f)
{
float xPlace = getX(radius, i) + oPos.x;
float yPlace = getY(radius, i) + oPos.y;
float zPlace = oPos.z + 2;
GameObject obj = Instantiate(cube, new Vector3(xPlace, yPlace, zPlace), Quaternion.identity);
obj.transform.rotation = Quaternion.Euler(0.0f, 0.0f, i);
obj.transform.parent = gameObject.transform;
}
make = false;
}
}
float getX(float r, float angle)
{
float x = r * Mathf.Cos(angle * Mathf.PI / 180.0f);
return x;
}
float getY(float r, float angle)
{
float y = r * Mathf.Sin(angle * Mathf.PI / 180);
return y;
}
}
How would I go about creating some more more arc's (not infinite obviously) based on the distance to 'main' object, and then remembering which ones to delete? (When you get to the end of the tunnel, the arc's behind player start dissapearing.
ex.png
(11.9 kB)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612100616im_/https://answers.unity.com/themes/thub/images/avi.jpg)