- Home /
I figured it out by myself
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.
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)?
@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.
@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);
Follow this Question
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