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 /
  • Help Room /
avatar image
0
Question by Maska77 · Feb 26, 2019 at 07:31 PM · instantiatelistif-statementsoverlap

Prevent spawning if the gameobject has same position as a list of Vector3s using another list?

I'm stuck on what to implement in my code to prevent the spawned game object to only spawn if my buildingList vectors3's positions don't match gridTakenlist Vectors. illiterate through the array doesn't seem to work, I want to avoid using colliders (colliders will solve the issue but I want to avoid using them for this project). Feeling frustrated, but I feel I'm close to the solution.

 private Vector3 rawPos;
 public GameObject mainObjectSpawner;
 private GameObject parentForBuildings;

 public GameObject lastGameObject;
 public Vector3 roundedPos;
 public List <Vector3> gridTakenList;

 public List<GameObject> buildingsList;

 void Start () {
     parentForBuildings = GameObject.Find("buildingsParent");
     if (!parentForBuildings) // if parentForBuildings doesn't exist, make it exist!
     {
         Debug.LogWarning("No parent for Buildings found! Creating one!");
         parentForBuildings = new GameObject("buildingsParent");          
     }  
 }

 void OnMouseDown() {
     //rawPos gets set in this method:
     mousePosRay(); 

     roundedPos = snapToGrid(rawPos);
     
     Debug.Log("Snap To Grid Vector: " + roundedPos);
         
        if(gridTakenList.Count <= 1)
     {
         Debug.LogWarning("Grid spot is open, placing building in position: " + roundedPos);
         buildingSpawner(roundedPos);
         buildingsList.Add((lastGameObject));
         gridTakenList = new List <Vector3>( new Vector3 [buildingsList.Count]);
         preventSameBuildingPosition();

     }
 }

 void mousePosRay() {
     RaycastHit hitInfo;
     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     if(Physics.Raycast(ray, out hitInfo))
     {
         
         //Debug.Log("The hit point is:" + hitInfo.point);
         rawPos = hitInfo.point;
     }

 }

 Vector3 snapToGrid(Vector3 rawWorldPos) {
     float newX = Mathf.RoundToInt(rawWorldPos.x);
     float newY = Mathf.RoundToInt(rawWorldPos.y);
     float newZ = Mathf.RoundToInt(rawWorldPos.z);
     return new Vector3(newX, newY += 0.50f, newZ);
     
 }


 void preventSameBuildingPosition() {
     
     for (int i = 0; i < buildingsList.Count; i++)
     {
         Vector3 vectorposition = buildingsList[i].transform.position;
         gridTakenList[i] = vectorposition;
         
     }
     
 }
 void buildingSpawner(Vector3 pos) {
     // the pos vector gets passed in as roundedPos
     // parentTransform gets passed in as 
     GameObject tempObject = Instantiate(mainObjectSpawner, pos, transform.rotation) as GameObject;
     tempObject.transform.parent = parentForBuildings.transform;
     lastGameObject = tempObject;
 }
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

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Maska77 · Feb 26, 2019 at 07:55 PM

Solved! 10 minutes after I posted I replaced OnMouseDown method with this code instead:

void OnMouseDown() {

     mousePosRay(); 

     roundedPos = snapToGrid(rawPos);
     
     Debug.Log("Snap To Grid Vector: " + roundedPos);
         
        if(gridTakenList.Contains(roundedPos))
     {

         Debug.Log("Spawn nothing!");
     }
     else
     {
         Debug.LogWarning("Grid spot is open, placing building in position: " + roundedPos);
         buildingSpawner(roundedPos);
         buildingsList.Add((lastGameObject));
         gridTakenList = new List<Vector3>(new Vector3[buildingsList.Count]);
         preventSameBuildingPosition();
         Debug.Log("Spawn");
     }       
 }
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

190 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 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

Trouble with Physics2D.OverlapArea detection 2 Answers

Shooting a bullet that stops in place when mouse is released 0 Answers

Instantiate working in Awake but on in Update 1 Answer

Object list is null? 1 Answer

How to Instantiate objects at random positions with a certain distance between each other? 2d 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