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 tomicz · Dec 30, 2015 at 04:53 AM · 2dendless runnersidescrollerinfinite runnerrandom spawn

Random obstacles spawn in 2D side endless runner game ?

Hello there.

I am new to Unity. I have developed scrolling backgrounds, floor, animation jump, slide and other things. The only thing left for now is an algorithm to spawn random objects, obstacles (I have created 3) that player has to dodge. My game has no platforms, it's all flat, so I don't need a random platform generator or similar.

Is there any idea, video tutorial or anything ? I am also new to programming C#, so it would be nice if you could explain with more details ? I am so eager to learn this!

I also checked for other similar questions, but couldn't find anything specific to my question. Also I seen some other tutorials, but they are mostly for 3D or random platform generators.

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
1

Answer by UsmanAbbasi · Dec 30, 2015 at 06:59 AM

If you know how to generate random platforms then you know how to spawn random objects/obstacles. Create obstacles in the same way as platforms with different positions and different scripts attached to them according to their behaviour.

Basic approach is to make prefab of obstacle gameobject and instantiate them on run time in similar way as you will instantiate platforms.

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 tomicz · Dec 30, 2015 at 08:00 AM 0
Share

Actually, platform script actually spawn 3 obstacles onve above each other at the same time, which is not what I need. I need something more like this

https://www.youtube.com/watch?v=RRu9d8VkcvA

avatar image
0

Answer by Shadowlash1221 · Dec 30, 2015 at 06:58 AM

How about making a cube gameobject and name it obstacles. Then flatten it on the y axes and expand it on the x axes, but make the cube invisible. Then add this script

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

Hope it helps!

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make the background follow the camera 5 Answers

2D infinite runner obstacle generation? 1 Answer

Warping Up after jumping? 0 Answers

Finding any objects below or above object at ANY POINT? 1 Answer

Running and Jumping problems of a 2D Endless Runner 2 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