Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SS2095 · Apr 21, 2014 at 05:36 PM · randomendless runnerobstacle

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SS2095 · Apr 22, 2014 at 10:50 AM 0
Share

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

avatar image SS2095 · Apr 22, 2014 at 11:01 AM 0
Share

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?

avatar image Scribe · Apr 22, 2014 at 04:03 PM 0
Share

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);
 }
avatar image SS2095 · Apr 22, 2014 at 04:34 PM 0
Share

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?

avatar image Scribe · Apr 22, 2014 at 04:39 PM 0
Share

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.

Show more comments
avatar image
0

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);
     }
 }

}

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image HTdeck · Aug 17, 2021 at 02:36 PM 0
Share

I have the same problem as him... @Scribe Thanks for any help given...

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Random Obstacle Placement 0 Answers

Make objects randomly fall 1 Answer

Spawn Different Obstacles for certain platforms 1 Answer

How can I make Random Obstacle Spawn randomize between 4 or more prefabs? 2 Answers

How to stop list from getting shorter ? [Beginner] 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges