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 /
This question was closed Nov 07, 2018 at 03:25 PM by TheEmeraldRuby for the following reason:

I figured it out by myself

avatar image
0
Question by TheEmeraldRuby · Nov 03, 2018 at 01:36 PM · physics2dcollider2dplatformoverlaprandomspawning

Checking for Colliders with Physics2D.OverlapBoxAll Won't Work?

In my 2D game, platforms are randomly spawned within a certain area. The player has to jump on these platforms to gain height. However, a platform will occasionally spawn too high above the one that the player is currently on, preventing them from gaining more height. To counter this, I attached an empty gameobject to the platform prefab with a y offset of 2 that should search a square or rectangular area above its platform for other platforms using Physics2D.OverlapBoxAll after its platform has found a final resting location (which is why there are 2 scripts, since the gameobject needs to know if its platform is done finding a spawn location). If it detects only 1 collider (the platform that it is above) it should spawn an extra platform within that space. However, nothing happens and I can't figure out what's wrong. The console doesn't even say "spawned a platform" Here are the scripts of the platform and its gameobject:

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlatformCfg : MonoBehaviour {
 
     bool canStayHere = true;
     public bool canCheckForLackOfPlatformsTop = false;
     public bool canCheckForLackOfPlatformsBottom = false;
     float moveHeight = 1;
     float platformsRadius = 1;
     float moveWidth = 1;
 
     GameObject player;
 
     // Use this for initialization
     void Start () {
 
         if (!CheckForPlatforms(transform.position)) // if spawn point is occupied already
         {
             Displace(transform.position);
         }
         player = GameObject.Find("Player");
     }
 
     // Update is called once per frame
     protected void Update()
     {
         if (player.transform.position.y - transform.position.y >= 5)
         {
             Destroy(this.gameObject);
         }
     }
 
     bool CheckForPlatforms(Vector2 positionToCheck)
     {
         Collider2D[] colliders = Physics2D.OverlapCircleAll(positionToCheck, platformsRadius);
         if (colliders.Length >= 1)
         {
             // if the 'positionToCheck' is already occupied by some platform
             return false;
         }
         else return true;
     } 
 
     void Displace(Vector2 currentPosition)
     {
         // some random position relative to 'currentPosition' parameter
         Vector2 randomPos;
         float randX = UnityEngine.Random.Range(currentPosition.x - moveWidth, currentPosition.x + moveWidth);
         float randY = UnityEngine.Random.Range(currentPosition.y - moveHeight, currentPosition.y + moveHeight);
         randomPos = new Vector2(randX, randY);
         // check this random position
         if (CheckForPlatforms(randomPos) && randomPos.y > 0) { // if it's not occupied and is higher than 0
                                            // take this place
             transform.position = randomPos;
             canCheckForLackOfPlatformsTop = true;
             canCheckForLackOfPlatformsBottom = true;
 
         }
         // otherwise, try to find another place relative to this randomPos
         // since it's occupied, so the platform can't be placed here
         else Displace(randomPos);
     }
 }


using System.Collections; using System.Collections.Generic; using UnityEngine;

public class PlatformDetectorTopCfg : PlatformCfg {

 float insHeight = 2;
 float insWidth = 2;

 public GameObject platform;

 // Use this for initialization
 void Start () {
     
 }
 
 // Update is called once per frame
 protected void Update() {

     if (canCheckForLackOfPlatformsTop) //checks the area for other platforms (and can spawn one) if this variable is true
     {
         Collider2D[] colliders = Physics2D.OverlapBoxAll(transform.position, new Vector2(insWidth, insHeight), 0);
         if (colliders.Length == 1)
         {
             float randX = UnityEngine.Random.Range(transform.position.x - insWidth, transform.position.x + insWidth);
             float randY = UnityEngine.Random.Range(transform.position.y - insHeight, transform.position.y + insHeight);
             Instantiate(platform, new Vector2(randX, randY), Quaternion.identity);
             Debug.Log("spawned a platform");
         }
         else canCheckForLackOfPlatformsTop = false;
     }
 }

}

If anyone knows what I'm doing wrong, please tell me! Thank you.

Comment
Add comment · Show 3
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 Nocktion · Nov 04, 2018 at 11:35 AM 0
Share

How high can the player can move on the y axis? I mean how much units can the player move upwards ? And how high the platforms spawn (the platforms you can't jump on)?

avatar image TheEmeraldRuby Nocktion · Nov 04, 2018 at 12:40 PM 0
Share

@Nocktion The player can jump about 4 units above the platform they are currently on which is why the detector is 2 units above the platform and searches an area with a height of 2 units upwards and downwards.

avatar image Nocktion TheEmeraldRuby · Nov 04, 2018 at 01:23 PM 0
Share

@TheEmeraldRuby Why don't you just spawn platforms according to this? I mean

For example if you have a platform spawning method call it SpawnPlatform(float x, float y) then you could just simply spawn them like this:

SpawnPlatform(player.transform.position.x + Random.Range(0.5f, 3.5f), player.transform.position.y + Random.Range(0.5f, 3.5f);

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

100 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

Related Questions

overlayCircle not working 1 Answer

Can't get game objects to instantiate at random positions without overlap - please help! 3 Answers

Physics2D.OverlapBoxNonAlloc not return all overlapped collider 1 Answer

How to Detect if a Collider is in the Area, Move Away, Rinse and Repeat? 1 Answer

Physics2D.OverlapCircleAll and OverlapCapsuleAll behaves differently? 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