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 Aug 15, 2018 at 06:42 PM by polan31 for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by polan31 · May 28, 2018 at 09:22 AM · 2d gamedistanceminimum

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
 }

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

  • Sort: 
avatar image
0

Answer by Unified2000 · May 28, 2018 at 10:40 AM

Some options:

  1. Turn off the renderer at the start of the game while you reposition all your objects.

  2. Don't manually place your objects in the editor, but instead instantiate them at the start of the game.

Comment
Add comment · Show 6 · 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
avatar image polan31 · May 28, 2018 at 11:10 AM 0
Share

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.

avatar image Unified2000 polan31 · May 28, 2018 at 01:12 PM 0
Share
 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);
         }
     }
 }
avatar image polan31 Unified2000 · May 28, 2018 at 01:39 PM 1
Share

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 :)

Show more comments
avatar image Harinezumi polan31 · Jun 01, 2018 at 12:00 PM 0
Share

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

Answers Answers and Comments

96 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

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


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