Answer the question
In order to leave comments, you need to log in
How to find empty space on water in 2d?
Good day!
I need to make a ship and bird spawner. I didn't come up with anything better than to create a variable "Dir", which stores the direction of the path of each monster (for ships y = 0, for birds = 3)... When 0, then we calculate the top point of the water and move the object there... But I have another problem, how to make the mobs not spawn on top of each other, I will attach the code below, the function that is responsible for the x position of the ships is called poolObj ()
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyController : MonoBehaviour
{
private Rigidbody2D _rb;
private GameCamera _gc;
private EnemyData _data;
private GameObject _water;
private int _minX;
private int _maxX;
public EnemyData Data
{
get
{
return _data;
}
set
{
_data = value;
}
}
public void Start()
{
_rb = GetComponent<Rigidbody2D>();
_gc = Camera.main.GetComponent<GameCamera>();
_water = GameObject.FindGameObjectWithTag("Water");
_minX = (int)Mathf.RoundToInt(1 - _gc.OffsetCamX) - 5;
_maxX = (int)Mathf.RoundToInt(Camera.main.ScreenToWorldPoint(_water.transform.position).x + Camera.main.ScreenToWorldPoint(_water.GetComponent<BoxCollider2D>().size * _water.transform.localScale).x);
_maxX = (int)Mathf.RoundToInt((1 - (_maxX + Camera.main.ScreenToWorldPoint(this.GetComponent<BoxCollider2D>().size * this.transform.localScale).x)) * 2) + 10;
float _waterY = Camera.main.ScreenToWorldPoint(_water.transform.position).y - (Camera.main.ScreenToWorldPoint(_water.GetComponent<BoxCollider2D>().size * _water.transform.localScale).y / 2);
float _yPos = _data.Dir.y > 0 ? _data.Dir.y : _waterY;
transform.position = new Vector3(transform.position.x, _data.Dir.y, transform.position.z);
GetComponent<Animator>().runtimeAnimatorController = _data.AnimController;
GetComponent<AudioSource>().clip = _data.WalkSound;
GetComponent<AudioSource>().loop = true;
GetComponent<SpriteRenderer>().sprite = _data.Skin;
transform.localScale = _data.ScaleEnemy;
if (_data.IsBird)
{
GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezePositionY;
transform.Rotate(0, 180, 0);
}
_poolObj();
}
private void FixedUpdate()
{
_rb.velocity += _data.Dir * _data.SpeedEnemy * Time.fixedDeltaTime;
if (transform.position.x < _minX)
{
_poolObj();
}
}
private void _poolObj()
{
int x = Random.Range(1 - _minX, _maxX);
List<int> _usedCoordX = new List<int>();
GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
foreach (GameObject enemy in enemies)
{
int _cx = (int)Mathf.RoundToInt(enemy.transform.position.x);
if (_cx != (int)Mathf.RoundToInt(transform.position.x))
{
_usedCoordX.Add(_cx);
_usedCoordX.Add(_cx - (int)Mathf.RoundToInt(Camera.main.ScreenToWorldPoint(enemy.GetComponent<BoxCollider2D>().size * enemy.transform.localScale).x));
_usedCoordX.Add(_cx + (int)Mathf.RoundToInt(Camera.main.ScreenToWorldPoint(enemy.GetComponent<BoxCollider2D>().size * enemy.transform.localScale).x));
}
}
int _checkX = (int)Mathf.RoundToInt(Camera.main.ScreenToWorldPoint(GetComponent<BoxCollider2D>().size * transform.localScale).x);
if (_usedCoordX.Equals(x) || _usedCoordX.Equals(x + _checkX) || _usedCoordX.Equals(x - _checkX))
{
x = Random.Range(1 - _minX, _maxX);
} else
{
<b></b> transform.position = new Vector3(x, transform.position.y, transform.position.z);
}
}
}
Answer the question
In order to leave comments, you need to log in
simple solution - loop through random variant X
// ВНЕ цикла подбора рандомного X для оптимизации
foreach (GameObject enemy in enemies)
{
// заполнить _usedCoordX
}
// цикл подбора рандомного X
while (true)
{
int x = Random.Range(1 - _minX, _maxX);
if (!_usedCoordX.Contains(x)) break;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question