Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 /
avatar image
0
Question by basavaraj_guled · Jul 29, 2015 at 11:53 AM · overlapmultiple objects

overlapping of enemy

hi there, i have script that has 2 public array variables called spawnPoints and spawnObjects, i have 9 spawnPoints and 4 spawnObjects. These 4 spawnObjects are placed on 4 spawnPoints. Here what i am doing is whenever i press the mouse button i m changing the position of spawnObjects to other spawnPoints, its working fine but the spawnObjects are overlapping some times.

please can anyone rewrite this code

 using UnityEngine;
  using System.Collections;
  
  public class Forward : MonoBehaviour
  {
          public GameObject[] spawnPoint;
          private GameObject spawn;
          public GameObject[] spawnObjects;
          private GameObject objectSpawn;
  
          void Update ()
          {
                  if (Input.GetMouseButtonDown (0)) {
                          
                          int a = Random.Range (0, spawnPoint.Length);
                          int b = Random.Range (0, spawnObjects.Length);
                          spawn = spawnPoint [a];
                          objectSpawn = spawnObjects [b];
                          objectSpawn.transform.position = spawn.transform.position;
                  }
          }
  }
 

 


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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by LawgiverSS2 · Jul 30, 2015 at 05:00 AM

To avoid duplicate random values, generate a random sequence between 0 and spawnPoints.Length (exclusive).

Use a shuffle algorithm such as Fisher Yates to generate your sequences:

 for (int i = length - 1; i > 0; i--)
 {
   int j = random.Next(i + 1);
   int temp = array[i];
   array[i] = array[j];
   array[j] = temp;
 }
Comment
Add comment · 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
0

Answer by NeverHopeless · Jul 30, 2015 at 06:48 AM

A possibility to fix this problem is to take an array for currently available positions for spawining, say AllowedPositions.

So, when the game starts:

 AllowedPositions = new int[] { 0,1,2,3,4,5,6,7,8 }

Now run:

 int a = Random.Range (0, AllowedPositions.Length)

and pick ath element from AllowedPositions and remove it from the array.

 int randomAvailablePosition = AllowedPositions[a];

e.g., Random.Range returns 3, then:

 AllowedPositions = new int[] { 0,1,2,   4,5,6,7,8 }

Since 3 is not in the AllowedPositions array so you can not pick this position again and overlapping will not occur.

A better option is to use List<T> since manipulation would be easy.

Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to make different animations affect different parts of the object? 2 Answers

Using a transparent shader and I'm getting graphical glitches with textures overlapping each other 1 Answer

Overlapping sprites in Unity2D 1 Answer

check box overlap / colliders in edit mode 0 Answers

Grab and drag object in physical and realistic way 2 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