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 TargonStudios · Jan 01, 2013 at 08:11 PM · gridplace

Placing Buildings

has anyone here ever played Empire Earth? If so How would i go about making a script where i could place buildings on the ground with the mouse (Like Empire Earth)? If you haven't ever seen/Played it, How would i be able to from a birds eye view be able to places buildings on the terrain? (The Buildings would be just clones of 3D models with box collides) Oh and a grid would be great! but if that's to hard, it doesn't matter.

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 Piflik · Jan 01, 2013 at 08:22 PM

You would have to do a raycast to the mouse position and you would get the collision with the ground plane (you can put the ground plane on a new layer and tell the ray to only collide with that layer, using a layer mask). You can then instantiate a new object at that position, or move an existing object there.

If you want to have a grid, this would also be possible. Just compare the position with the position of the tiles and chose the lowest distance...the actual implementation of this test might be a bit complicated, just iterating through an array would be suboptimal...I'd suggest a Quad-Tree, but I guess that's too much for a beginner...

For the raycasting you can have a look at this...it's UnityScript...

 private var mousPos : RaycastHit;                    
 private var positionOnGroundplane : Vector3;
 private var layerMask  = 1 << 8;
 
 
 function Update() {
     if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), mousPos, Mathf.Infinity, layerMask)){
         positionOnGroundplane = mousPos.point;
     }
 }

I cut this from an old script of mine...not sure if it is bugfree, since I trimmed a whole lot of it away.

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 TargonStudios · Jan 01, 2013 at 10:36 PM 0
Share

Unexpected token: Vector3

avatar image Piflik · Jan 01, 2013 at 10:39 PM 0
Share

Oh, sorry...I added a new Vector3 variable and accidentally used c# syntax...fixed it now.

avatar image TargonStudios · Jan 01, 2013 at 11:50 PM 0
Share

So how do i work it? what do i attach the script to? How do i set the thing it places?

avatar image TargonStudios · Jan 02, 2013 at 11:37 PM 0
Share

Oh And would i be able to place more then one thing? like press "1" to place somthing and "2" to place another thing?

avatar image Piflik · Jan 02, 2013 at 11:42 PM 0
Share

The script is not complete. All it does is a raycast and get the point on the ground plane. What you do with that point is up to you. You can place whatever you want there.

Where you place this script doesn't matter, either, it just needs to be on an object that is present in the scene. The camera would make sense, or an empty that is used as a 'Build-Objects-$$anonymous$$anager'. Also you would probably not do the raycast on Update, but in a separate function that is called on $$anonymous$$ouse Input. The script was mainly meant to give you an idea how raycasts work.

Show more comments
avatar image
0

Answer by TargonStudios · Jan 03, 2013 at 06:28 PM

Ok Well Ive found a script And did a little to it, it works!... but not how i want it to. When i try to place a building, it places it next to some other object (Another building for example) and not where i clicked. Ive tried layers, everything! (I found this script on a Minecraft clone thing if that helps) heres the script:

 var blockLayer : LayerMask = 1;      
 var range : float = Mathf.infinity;
 var hit : RaycastHit;
 var Building : GameObject;
 
 
 function Update() {
 if (Input.GetMouseButtonDown(0)) Build();
 }
 
 function Build() {
 if (HitBlock()) {
 var cube = Instantiate(Building);
 cube.transform.position = hit.transform.position + hit.normal;
 }
 }
 
 function HitBlock() : boolean {
 return Physics.Raycast(transform.position, transform.forward, hit, range, blockLayer);
 }
 
 
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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I distribute cells in a grid with the new UI from script? 1 Answer

[Script] place block(prefab) align to grid like minecraft? 4 Answers

Creating an Interactable Grid for Tactics Game (C#) 1 Answer

Script is working incorrectly. (rotation) 1 Answer

Help with Grid movement script + AI script 1 Answer


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