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 SpaceyJ · Nov 24, 2018 at 01:55 PM · 2dphysicsfor-loopsnakeoptimisation

Optimising code for spawning food in snake game

I'm making a snake game and need to prevent the food from spawning inside the snake or the walls of the map (there are also walls inside the map). I am currently achieving this by creating a list of all the possible coordinates for a food object to spawn in whenever the snake eats a food item and a new one must be spawned. This seems to be very inefficient as I get a huge frame drop whenever food is spawned. I was wondering if there is a better way to find a location that does not collide with the snake or the walls. Any help would be very much appreciated :)

Some other info on my game for context: the spawning of food and the location of the snake is quantized to 10 coordinate divisions. This allows smooth movement of the snake however the snake is always part of a rigid 12 by 10 grid.

 public GameObject foodPrefab;

 public Transform borderTop;
 public Transform borderBottom;
 public Transform borderLeft;
 public Transform borderRight;

 // Use this for initialization
 void Start () {
     Spawn ();
 }

 public void Spawn () {
     List<Vector2> spawn_locations = new List<Vector2>();

     for (int x = -120; x < 120; x = x + 10) {
         for (int y = -100; y < 100; y = y + 10) {
             if (Physics2D.OverlapCircle(new Vector2(x, y), 0.5f) == false) {
                 spawn_locations.Add (new Vector2 (x, y));
             } 
         }
     }

     Vector2 coords = spawn_locations[Random.Range(0, spawn_locations.Count)];

     Instantiate (foodPrefab, new Vector3(coords.x, coords.y, 0), Quaternion.identity);
 }   
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

Answer by toddisarockstar · Nov 24, 2018 at 05:27 PM

the OverlapCircle is slowing you down when you are calling it 480 times!!!!! a better approach would be: choose and random space, call the overlap once. if it is clear, you are done.

if not.... simple choose another random till you get a clear space. instead of doing 480 checks this should give you the same effect and you should be doing just a couple checks per spawn!!!!!!

the loop in this function repeats till it gets a good spot

         public void Spawn () {
             int checks = 0;
             int x = 0; int y=0;
             while (checks <5000) {
                 checks++;
                 // get random coordinate between -120 and  + 120.
                 x = Random.Range (0, 25) - 12;x *= 10;
                 y = Random.Range (0, 21) - 10;y *= 10;
                 if(!Physics2D.OverlapCircle(new Vector2(x, y), 0.5f)){break;}
             }
         Instantiate (foodPrefab, new Vector3(x, y, 0), Quaternion.identity);
 
             print("I checked " + checks + " places before i found a spot");
         }

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

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

Related Questions

2D (With Planes) Jump Script 0 Answers

2d game platform physics 1 Answer

How can I make a game object move in parabolic motion as if it were under gravity? 2 Answers

Physics in 2d arkanoid. Tangential and normal velocity 1 Answer

Velocity Movement & Physics Interactions by Rigidbody2D 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