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 CodingNoob · Apr 29, 2015 at 08:41 AM · spawnobstaclerunner

2D Infinite Runner - prevent obstacle from spawning over gap

Hey there,

I'm dealing with a new issue and I couldn't find a good solution to it by now, so I hope someone of you might help me.

The thing is, in my 2D infinite Runner, I got obstacles spawning randomly on the way, but I don't want them to spawn to close or even inside a gap. My first attempt was to create empty gameobjects with a collider to mark gap areas in which the obstacles shouldn't spawn. So if an obstacles spawns inside such an area, it would be moved to the right until it left the area. (I achieved this with an Physics.Overlap function which checks the collision with a certain layer and controlls a bool. As long as the bool is set to true, the obstacle is moved). The problem with this is, that it might happen that if two gaps are following up, the obstacle will be stuck inside the second gap anyways because it cannot move any further (the obstacles spawn above the floor and fall down, so when the overlap bool is true they're pushed to the side as long as they do not touch the ground). I thought about simply destorying the obstacle if it spawns inside a gap, but I couldn't figure out how to to that to only this one specific obstacle...if I tell the script to destroy the gameobject, all obstacles are completely destroyed...(they all have the same script attached to check the gap overlap).

Would be great if anyone could help me out.

Thank you in advance!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Teku-Studios · Apr 29, 2015 at 09:10 AM

Without looking at your code is hard to say. If you have already spawned a gap, why should an obstacle spawn at that same position? You might save all the positions with hazards (whether it is a gap or an obstacle) and never spawn anything in that position again.

if you have a list or an array of hazards, you could tell your code to always spawn a new hazard in the next position. So let's say that you have a size 6 array of fixed positions with spawned objects linked to them like this

 [gap, obstacle, gap, gap, null, null]

Your next hazard, gap or box, should spawn at the next position, preventing it from being placed over an already existing hazard. This is a very rough idea, I'm afraid I can't help further without deeper knowledge of your code setup.

Since it is an endless runner kind of game, you could clear the hazards array/list once it's full so you don't have a potentially infinite variable that could destroy your memory in the long run. But again, this is just the first solution I've come up with, and I assume it's far from ideal.

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 CodingNoob · Jun 11, 2015 at 01:38 PM 0
Share

Well I creaeted the game based on this tutorial:

http://www.raywenderlich.com/69392/make-game-like-jetpack-joyride-unity-2d-part-1

So basically I have the floors generated and then I have the obstacles generated in two different lists. The floors are whole sets of different smaller pieces (e.g. straight, gap, transition to higher level, high straight, gap, transition to normal level...) The obstacles spawn on a higher position and fall on to the ground. That's why they can spawn above a gap.

avatar image
0

Answer by kskjadav007 · Sep 08, 2017 at 05:53 AM

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

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

Answer by N00MKRAD · Sep 08, 2017 at 06:19 AM

Just let the obstacle check if there's a ground (with raycast or something else) on Start() and Destroy it if not.

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

22 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

Related Questions

Please share ideas for obstacle management 0 Answers

3D Infinite runner obstacle creation 0 Answers

How to have IEnumerators run but not in Update 2 Answers

Spawning obstacles randomly in 3D endless runner game 0 Answers

How to spawn different object from Prefabs folder 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