Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Damien220 · May 16, 2018 at 06:16 PM · 2d gamelogicobstacle

What logic should i implement in this 2D game?

Hello everyone, I would like to ask about a game i am making,it is a about a 2D game where the player goes from up to down, and there are obstacles being generated randomly while the background(which is the movable object) moves up. My question is: what logic should i implement in regard for obstacle generation?My current implementation (which is wrong) is creating a queue holding the objects and then comparing the position of the item on peek of the queue with the distance of the player and then creating a new object,but this implementation keeps generating objects non-stop and this ruin the game,So how should i think about that. Till now this is my code regarding the obstacles: using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class ObstacleManager : MonoBehaviour {
     public float scrollSpeed;
     public int numberOfObjects;
     public float recycleOffset;
 
     public Transform prefab;
     public Vector3 minSize, maxSize, minGap, maxGap;
     public float minY, maxY;
 
 
     private static Vector3 startPos;
     private Vector3 nextPosition;
     private Queue<Transform> objectQueue;
 
 
     // Use this for initialization
     void Start () {
         GameEventManager.GameStart += GameStart;
         GameEventManager.GameOver += GameOver;
         objectQueue = new Queue<Transform>(numberOfObjects);
         enabled = false;
     }
     
     // Update is called once per frame
     void Update ()
     {
 //        if (objectQueue.Count > 0 && prefab != null) {
 ////            Debug.Log("1: "+objectQueue.Peek().localPosition.y + recycleOffset);
 ////            Debug.Log("2: "+-(PlayerController.distanceTraveled));
 ////            Debug.Log(objectQueue.Peek().localPosition.y + recycleOffset < -(PlayerController.distanceTraveled));
 //        if(objectQueue.Peek().localPosition.y + recycleOffset < -(PlayerController.distanceTraveled)){
 //            Recycle();
 ////            Debug.Log(prefab.position);
 //            }
 //        }
 
 
     }
 
 
     public void ObsctaclesMovement (float speed)
     {
         float newPos = Mathf.Repeat(Time.time * speed,10);
         transform.position = startPos + Vector3.up * newPos;
     }
 
     private void GameStart () {
         nextPosition = startPos;
         for (int i = 0; i < numberOfObjects; i++) {
             objectQueue.Enqueue((Transform)Instantiate(
                 prefab, new Vector3(prefab.position.x, 0f, 0f), Quaternion.identity));
         }
         for(int i = 0; i < numberOfObjects; i++){
             Recycle();
         }
         enabled = true;
     }
 
     private void GameOver () {
         enabled = false;
     }
 
     private void Recycle () {
         Vector3 position = nextPosition;
         Vector3 scale = new Vector3(
             Random.Range(0.5f, 6.5f),
             Random.Range(-1.5f, -16f),
             0f);
         position.x = scale.x;
         position.y = scale.y;
 
         Transform o = objectQueue.Dequeue();
         o.localPosition = position;
 
         objectQueue.Enqueue(o);
 
         nextPosition = new Vector3(
             Random.Range(0.5f, 6.5f),
             Random.Range(-1.5f, -16f) ,
             0f);
 //            Debug.Log("nex position: "+nextPosition);
 
 
     }
 }
 
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

1 Reply

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

Answer by greencheese122 · May 17, 2018 at 10:27 AM

Use a coroutine

 public Ienumerator spawn(){
 While(true){
 //Do Staff where
 Yield return new WaitForSeconds(prof seconds);
 }
 }

And Then call it in start fiction with StartCoroutine(spawn());

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 Damien220 · May 19, 2018 at 04:15 PM 0
Share

you mistyped Yield ....it's not Field

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

94 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 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 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 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 avatar image avatar image

Related Questions

problems with generation of obstacles help 1 Answer

Sprite turning around repeatedly against a wall. How to have "Wallslide" flip sprite? 0 Answers

What is the infinite loop in the following code? 0 Answers

Game over funcition isn't being called accurately? 2 Answers

2D repeating obstacles with a random Y value 0 Answers


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