Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by helloiam · Jan 16, 2014 at 12:38 PM · objectsgridtile

Instantiate an object next to an object

Hello programmers,

I would like to implement that when I have placed an object let's say on a grid and if I click on the next tile they are coming inside each other. So what I think that should work is to check all the sides of the object and if it has collision, then place it next to the object automatically.

Here is a snippet of my code:

 if (EditorManager.instance.value == EditorManager.Mode.UIMode) {
 
             RaycastHit hit;
 
             if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100))
             {
                 if (Input.GetMouseButton(0))
                 {
                     try 
                     {
                         int x = hit.collider.GetComponent<Tile>().getX;
                         int y = hit.collider.GetComponent<Tile>().getY;
 
                         mapxml[x, y].SetTileType(1);
 
                         wallObject = MonoBehaviour.Instantiate(wall, new Vector3(hit.transform.position.x, 
                                                                                             hit.transform.position.y + 0.5f, hit.transform.position.z), Quaternion.identity) as GameObject;
                         bool valid = true;
                         
                         foreach (GameObject b in walls) 
                         {
                             if (wallObject.transform.collider.bounds.Intersects(b.collider.bounds))
                             {
                                 GameObject.DestroyImmediate(wallObject);
                                 valid = false;
                                 break;
                             }
                         }
 
                         if (valid && wallObject != null)
                         {
                             walls.Add(wallObject);
                         }
 
                         SetMap();
                     }
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

Answer by frogsbo · Jan 16, 2014 at 01:05 PM

if you have less than 10 000 objects to place on grid, put them in an array, and after racast has position, compare any new tiles to positions of tiles already in array, and if same, place it on x+1 and check also if there is one tile there. use tags for example for tiles, find object with tag tile, for each tile in tiles... if pos = pos tile... etc

other option is sphere cast. cast a sphere physics around the position and check all tiles a spherical radius of for example 2.

Comment
Add comment · Show 4 · 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 helloiam · Jan 16, 2014 at 01:24 PM 1
Share

I have them currently in a list where I add them and when they have intersection I simply remove it but what's the next step?

avatar image helloiam · Jan 16, 2014 at 01:34 PM 1
Share

I have updated my script so take a look and tell me what is next

avatar image MountDoomTeam · Jan 16, 2014 at 06:20 PM 0
Share

Great work! i would check if the clicked position already has a tile before making one there and then destroying it if you have to. it saves a step

 I would do this:
 
 get hitpoint.position
 
 var alreadythere :int = 0;
 for wall in walls{
  if(wall.position == hit point){ alreadythere =1}
 }
 
 if alreadythere = 0, instantiate new tile on hitpoint.
 
 if alreadythere <=1, instantiate new tile on (hitpoint)+Vector3(1,0,0)//simple way to do it
 ______________________________________________________________
 
 complex way to do it...
 
 if alreadythere <= 1, make 4 loops to check the neighbour spaces if there is a tile there. if one of them is alreadythere=0, instantiate tile on first free space 

out of 4 neigbours, even 8 if you want i already did alot of this with this one:" http://www.youtube.com/watch?v=$$anonymous$$HpIb6iDv$$anonymous$$Q

avatar image helloiam · Jan 17, 2014 at 08:22 AM 0
Share

The video is indeed a great example what I almost achieved but I made it more complexer then you would think, anyway I almost fixed it by myself so thanks for commenting and trying to point me in the right direction

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

20 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

Related Questions

How to apply more than one trigger on a single gamebject 1 Answer

Grid Movement VS Free Movement 1 Answer

2D Tile Map Question 0 Answers

Generating and altering a mesh at runtime as a grid based terrain 3 Answers

Clicking on objects 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