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 Ballisticspork · May 09, 2017 at 06:13 AM · platformergenerationendless runnerlimitations

Platform Generation For Endless Runner

Alrighty, so, I've got a bit of a problem. I'm trying to make an endless runner (2d) that consists of 5 platforms. The platforms are numbered 0-4 and correspond to their number in the platforms[] array (Platform0 = Platforms[0] and so on). However, I drew the platforms in such as way that each platform can only be next a few of the other platforms (for example, plat0 can only be next to plat1 or plat2). The first code I put below (script that works) was the script without the 'rules' telling which platform could be placed. That script worked fine, unlike the second script posted below that. That script, when run, immediately and continuously spawns platforms at location x =16. (And by the way all platforms are exactly 4 units long which is why in the scripts the platforms are instantiated 4 units further than the last) I hope someone can figure this out!

Thanks!

Script that works: // Use this for initialization void Start () { player = GameObject.FindGameObjectWithTag("Player"); lastPlaced = startPlatform; }

 // Update is called once per frame
 void Update ()
 {
     distanceToPlayer = Vector2.Distance(player.transform.position, lastPlaced.transform.position);

    platToPlace = platforms[Random.Range(0, platforms.Length)];

     if (distanceToPlayer < 8)
     {
         placed = Instantiate(platToPlace, new Vector2(lastPlaced.transform.position.x + 4, lastPlaced.transform.position.y), lastPlaced.transform.rotation);
         lastPlaced = placed;
     }
 }

}

Script that doesn't work: // Use this for initialization void Start () { player = GameObject.FindGameObjectWithTag("Player"); lastPlaced = startPlatform; }

 // Update is called once per frame
 void Update ()
 {
     distanceToPlayer = Vector2.Distance(player.transform.position, lastPlaced.transform.position);

    platToPlace = platforms[Random.Range(0, platforms.Length)];

     if (lastPlaced = platforms[0])
     {
         platToPlace = platforms[Random.Range(1,2)];
     }
     if (lastPlaced = platforms[1])
     {
         platToPlace = platforms[0];
     }
     if (lastPlaced = platforms[2])
     {
         platToPlace = platforms[Random.Range(3,4)];
     }
     if (lastPlaced = platforms[3])
     {
         platToPlace = platforms[0];
     }
     if (lastPlaced = platforms[4])
     {
         platToPlace = platforms[3];
     }

     if (distanceToPlayer < 8)
     {
         lastPlaced = Instantiate(platToPlace, new Vector2(lastPlaced.transform.position.x + 4, lastPlaced.transform.position.y), lastPlaced.transform.rotation);
     }
 }

}

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
0
Best Answer

Answer by loafer · May 16, 2017 at 04:35 PM

There are some syntax errors in your conditionals.

If you are not checking for equality you are setting them equal

 if (lastPlaced = platforms[0]) 

will always be true because you are only using a single equal.

Because you aren't using if / else if every single one of those conditionals will run and you will end up this every single time:

 platToPlace = platforms[3];

And then your final conditional will run if distanceToPlayer < 8.

You should use if / else if and also check for equality using

 if( lastPlaced == platforms[4] ) { } 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Endless Runner world generation stops at certain point. 2 Answers

Random Obstacle Placement 0 Answers

Hey how can I spawn a platform after the previous one. 0 Answers

What is a good way to randomly generate clouds along a path? 0 Answers

Tiles spawn slightly to the right of my player 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