- Home /
random obstacle generator
in a 3 lane system i want the obstacles to generate upto a certain distance in front of the player,but the script i've made isn't working.take a look
using UnityEngine; using System.Collections;
public class Obstacle_generate : MonoBehaviour {
// Use this for initialization
public Transform obstacl;
public float y;
public float[] chooseX;
public int randomX;
public int pp;
void Start () {
chooseX=new float[3];
chooseX [0] = -1.5f;
chooseX [1] = 0;
chooseX [2] = 1.5f;
}
// Update is called once per frame
void Update () {
label1:pp = (int)loco.playerDistance;
randomX = Random.Range(0,3);
if(pp%10==0)
{
y = Random.Range(pp,pp + 30);
Instantiate (obstacl, new Vector3 (chooseX[randomX], y,0),Quaternion.identity);
goto label1;
}
}
}
where loco.playerDistance is the y coordinate of the player object it spawns too many obstacles sometimes even on top of each other how can i make it spawn after a specific distance each and only once at that y coordinate
Answer by Scribe · Apr 21, 2014 at 06:10 PM
This is untested as I'm away from my normal computer atm, but it should give you an idea of how to do it I hope:
public float yDistance = 10;
public float minSpread = 5;
public float maxSpread = 10;
public Transform playerTransform;
public Transform obstaclePrefab;
float ySpread;
float lastYPos;
void Start(){
lastYPos = Mathf.NegativeInfinity;
ySpread = Random.Range(minSpread, maxSpread);
}
void Update () {
if(playerTransform.position.y - lastYPos >= ySpread){
float lanePos = Random.Range(0, 3);
lanePos = (lanePos-1)*1.5f;
Instantiate(obstaclePrefab, new Vector3(lanePos, playerTransform.position.y + yDistance, 0), Quaternion.identity);
lastYPos = playerTransform.position.y;
ySpread = Random.Range(minSpread, maxSpread);
}
}
Just drag your player object and obstacle prefabs onto the appropriate fields in the inspector.
it's working great thanks.but i want the obstacles to appear after a specific y distance.can you help me with that.i really can't thank you enough for this.thanks for the quick reply.also,can you tell me why my script wouldn't work?i'm trying to learn from my mistakes
i also have this script attached to the obstacles in order to destroy them when not needed
using UnityEngine; using System.Collections;
public class Destroy_obstacle : $$anonymous$$onoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (loco.playerDistance > transform.position.y) {
Destroy (this.gameObject);
}
}
} but when i use this ,the generator stops generating ant any more obstacles.some help?
Hi, so here's a quick update to the script:
public float startDistance = 10;
public float yDistance = 100;
public float $$anonymous$$Spread = 5;
public float maxSpread = 10;
public Transform playerTransform;
public Transform obstaclePrefab;
float ySpread;
float lastYPos;
void Start(){
ySpread = Random.Range($$anonymous$$Spread, maxSpread);
lastYPos = playerTransform.position.y + (startDistance - ySpread - yDistance);
}
void Update () {
if(playerTransform.position.y - lastYPos >= ySpread){
float lanePos = Random.Range(0, 3);
lanePos = (lanePos-1)*1.5f;
Instantiate(obstaclePrefab, new Vector3(lanePos, lastYPos + ySpread + yDistance, 0), Quaternion.identity);
lastYPos += ySpread;
ySpread = Random.Range($$anonymous$$Spread, maxSpread);
}
}
Changing startDistance should change how far ahead of the player, objects start and hopefully changing the yDistance variable will control how far ahead of the player, objects are spawned.
As for why your first attempt was incorrect, when the playerDistance was 10 or 10.1 or 10.4 or 10.3222223323 your if would return true, so there was a range of time between certain positions where it would keep instantiating new objects. It is basically equivalent to:
while(loco.playerDistance > 9.5 %% loco.playerDistance < 10.5){
Instantiate();
}
Your last problem I am not too sure about. Is this code attached to the same object as where you are attaching the code I have given you, if so you are deleting the object that runs the instantiations.
Otherwise add some debugs:
if (loco.playerDistance > transform.position.y) {
Debug.Log("Player distance: " + loco.playerDistance);
Debug.Log(gameObject + " is at height " + transform.position.y);
Destroy (gameObject);
}
yup,it's working .i have created an empty object called obstacleGenerator to which your script is attached.then there is another object called obstacle which i've referenced to the script.what's happening is it deletes the object obstacle and hence the script doesn't know what to instantiate.how can i overcome this?
Ahh, save your obstacle object as a prefab. Set up an obstacle with your deletion script on it and put it at position (0, 0, 0) and then drag that whole object into your Project window and it should create what is known as a prefab. Then set the variable on the obstacleGenerator to the obstacle prefab. That way, the instantiated objects are clones of the prefab and not the original, so the original is never deleted.
Answer by RigorousSine759 · Dec 09, 2019 at 12:50 PM
hey, i just read this, and ive copied and pasted it but i have a problem, it says : Assets/Scripts/Ramdom.cs(37,16): error CS0103: The name `loco' does not exist in the current context What do I have to do? Thx Here is my script btw using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Ramdom : MonoBehaviour {
public float startDistance = 10;
public float yDistance = 100;
public float minSpread = 5;
public float maxSpread = 10;
public Transform playerTransform;
public Transform obstaclePrefab;
float ySpread;
float lastYPos;
void Start()
{
ySpread = Random.Range(minSpread, maxSpread);
lastYPos = playerTransform.position.y + (startDistance - ySpread - yDistance);
}
void Update()
{
if (playerTransform.position.y - lastYPos >= ySpread)
{
float lanePos = Random.Range(0, 3);
lanePos = (lanePos - 1) * 1.5f;
Instantiate(obstaclePrefab, new Vector3(lanePos, lastYPos + ySpread + yDistance, 0), Quaternion.identity);
lastYPos += ySpread;
ySpread = Random.Range(minSpread, maxSpread);
}
while (loco.playerDistance > 9.5 % loco.playerDistance < 10.5)
{
Instantiate();
}
if (loco.playerDistance > transform.position.y)
{
Debug.Log("Player distance: " + loco.playerDistance);
Debug.Log(gameObject + " is at height " + transform.position.y);
Destroy(gameObject);
}
}
}
I have the same problem as him... @Scribe Thanks for any help given...