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
0
Question by Mentaner · Aug 04, 2014 at 07:30 PM · performancespawnmousepositionfloorspawnpoint

Spawn single floor tile at mouse position

Hello, I am trying to build an RCT, Prisonarchitect or Sim like game in Unity2D.

The idea is that I press a UI button, get a tile that follows the mouse and by pressing the mouse button a clone of that tile spawns at the mouse(x;y) location.

This should not be a problem since this is pretty much basic.

But When I try to place a single tile I get a ton of tiles in the same place. Somehow I need way to scan for an already existing tile (or other game_object) that is already placed at that location.

I have tried it with "Tags" but I could not get to work. My script so far is also for unity3D I have tried to modify it so that it don't need the Vector3 component but it did not work either.

I have no idea. I am desperate right now. The solution can't be that difficult but I am not able to see it.

-Thanks

 var depth = 10.0;
 
 var FloorPrefab : Transform; 
                
 
 function Update () 
 {
     //Follow mouse
     var mousePos = Input.mousePosition;
     var wantedPos = Camera.main.ScreenToWorldPoint (Vector3 (mousePos.x, mousePos.y, depth));
     transform.position = wantedPos;
 
     {
         //Spawn object at mouse position
         var mousex = Input.mousePosition.x;
         var mousey = Input.mousePosition.y;
 
         var ray = camera.main.ScreenPointToRay (Vector3(mousex,mousey,0));
 
         var hit : RaycastHit;
 
     }
         if ( Input.GetMouseButtonDown(1) )
     {
         Destroy(gameObject);
     }
            if (Physics.Raycast (ray, hit, 200)) 
            {
             if ( Input.GetMouseButtonUp(0) )
             {
             var create = Instantiate(FloorPrefab, hit.point, Quaternion.identity);
             } 
         }
     }  
 
 


Comment
Add comment · Show 5
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 AW0610AUT · Aug 04, 2014 at 07:50 PM 0
Share

Hmm, not really sure but do you have the script attached to the floortile that you are spawning? I'm asking because i think maybe when you instantiate one floortile in the script that script on the instantiated floortile spawns a new one and so on...

avatar image Mentaner · Aug 04, 2014 at 08:47 PM 0
Share

Yes the script is attached to the floortile. But this should not be the problem. If I understand this correct then I have to make sure that the script only spawns a new tile if there is no other game_object in the way. But how ??

avatar image AW0610AUT · Aug 04, 2014 at 08:52 PM 0
Share

You can check the desired position for and already existing object with a Raycast. Read this: http://docs.unity3d.com/ScriptReference/RaycastHit-collider.html

You can use the .collider info that you get to deter$$anonymous$$ if there is a object with (for example) the tag "floortile".

avatar image DoctorWho · Aug 04, 2014 at 09:25 PM 0
Share

This may help you. manual API, prefab.

avatar image DoctorWho · Aug 04, 2014 at 09:30 PM 0
Share

Also in line 25 the mouse button up? looks like it should be a mouse button down, it looks like this input is checking to see if the button is not pressed, then placing object.

1 Reply

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

Answer by Mentaner · Aug 05, 2014 at 10:49 PM

Thank you!

I have done it with tags on colliders

Works like a Dream!

 function Update () 
 }
            if (Physics.Raycast (ray, hit) && hit.collider.gameObject.CompareTag("Ground")) 
         {
             if ( Input.GetMouseButtonDown(0) )
             {
             var create = Instantiate(FloorPrefab, hit.point, Quaternion.identity);
             } 
         }
 }

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 do I spawn a prefab where I click? 2 Answers

Multiple spawn points (C#) 2 Answers

¿can i draw Objects only when in a certain range around the Player? 2 Answers

UNET - Server is spawned in correct spawn position but players are spawned "half way" 0 Answers

Player Singleton and Spawning in new scene issue 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