- Home /
Problem is not reproducible or outdated
How to determine the minimum distance between objects?
I have 2d simple tutorial game.
The game has several objects.
I would like the objects to change their position each time PLAY is pressed.
The idea is that each time you start the game, they are in a different place- but without each other collision.
The problem is that if there are many objects, their position change is visible.
I.e, if the object is loaded on the second object, you can see how it changes positions.
Is it possible to simply set the minimum distance so that objects just don't load on other objects?
My script now:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RandomRange : MonoBehaviour {
float x;
float y;
Vector2 position;
void Start()
{
x = Random.Range(-4, 4);
y = Random.Range(-4, 4);
position = new Vector2(x, y);
gameObject.transform.position = position;
}
public static class PhysicsEx
{
public static bool CheckBounds2D(Vector2 position, Vector2 boundsSize, int layerMask)
{
Bounds boxBounds = new Bounds(position, boundsSize);
float sqrHalfBoxSize = boxBounds.extents.sqrMagnitude;
float overlapingCircleRadius = Mathf.Sqrt(sqrHalfBoxSize + sqrHalfBoxSize);
Collider2D[] hitColliders = Physics2D.OverlapCircleAll(position, overlapingCircleRadius, layerMask);
foreach (Collider2D otherCollider in hitColliders)
{
if (otherCollider.bounds.Intersects(boxBounds))
return (false);
}
return (true);
}
}
}
Also, someone has given me another script that can work, but it does not work:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CPlacementManager : MonoBehaviour {
public List<Vector2> Positions { get; private set; }
void StartPlay( )
{
Positions = new List<Vector2>();
for ( int nPlaced = 0 ; nPlaced < numberToPlace; ++nPlaced)
{
bool bPlaced = false;
while( !bPlaced )
{
bPlaced = true;
int nCheck = 0;
Vector2 vNextTry = placementCentre + new Vector2(
Random.Range(-halfBounds.x, halfBounds.x)
, Random.Range(-halfBounds.y, halfBounds.y));
while(bPlaced && nCheck < nPlaced )
{
if ( (Positions[nCheck] - vNextTry).sqrMagnitude < minSqrSeparation )
bPlaced = false;
}
if (bPlaced)
Positions.Add(transform.position);
}
}
}
#pragma warning disable 649
[SerializeField] Vector2 halfBounds = new Vector2( 4, 4 );
[SerializeField] Vector2 placementCentre;
[SerializeField] int numberToPlace = 10;
[SerializeField] float minSqrSeparation = 1f;
#pragma warning restore 649
}
Answer by Unified2000 · May 28, 2018 at 10:40 AM
Some options:
Turn off the renderer at the start of the game while you reposition all your objects.
Don't manually place your objects in the editor, but instead instantiate them at the start of the game.
It's a bad idea-the objects I have are different, each has a different shape and number on them, so I have to put them in manually.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RandomizePositions : $$anonymous$$onoBehaviour {
// Use this for initialization
void Start () {
for (int i=0;i<20;i++)
{
GameObject obj=GameObject.CreatePrimitive(PrimitiveType.Cube);
obj.tag="cheese";
}
Randomize();
}
void Randomize () {
GameObject[] objects=GameObject.FindGameObjectsWithTag("cheese");
for (int n=0;n<objects.Length;n++)
{
objects[n].transform.position=new Vector3(n,0,Random.value*objects.Length);
}
}
}
Hey.
Thank you very much for your answer.
I don't want to be rude, but it seems to me that your answer is not on the subject.
I wrote that: Is it possible to simply set the $$anonymous$$imum distance so that objects just don't load on other objects?
Your answer is about creating completely new objects ...
I'm sorry, I hope I didn' offend you. I would just like to lead you out of error :)
$$anonymous$$aybe you didn't understand something, because my English is on a low level :)
Or I didn't understand something in your answer :) If so, I'm sorry :)
First of all, to answer your subject: no, there is no simple way to set the $$anonymous$$imum distance. The way to do that is to position the objects using a script, usually when created.
Unified2000's ideas are good, either of those should work, however, there is a bit of confusion how you setup your objects: you say that you place them manually, but then would like them to have different position every time you play. So could you please describe how your scene is set up? Is it that you place the objects into the scene, but then you would like them to move when you start the scene? In that case, you could do what Unified2000 said, and deactivate their renderers in the first frame. It is unclear what is the problem with that solution.
Another solution is that you don't place the objects into the scene, but let a script create them and position them. The script could have a reference to all of the different object types so it could create one or more from each.
Follow this Question
Related Questions
how move cube in horizontal and vertical movement in unity using arduino 0 Answers
distance shortest in the list 1 Answer
FPS Controller Minimum Movement Increasing 1 Answer
Instantiate gameobject with minimum distance from another gameobject 1 Answer
Should i use terrain generation or make it myself for android? 0 Answers