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
-2
Question by Carbongrip · Jan 02, 2014 at 03:02 AM · c#objectsbuildingplacement

Objects Stuff

So I want to make a game where I can place things and build and stuff like the old Star Wars Galaxies game. So how can I make a object follow the mouse around and then on mouse click place the object at that point. Also it would be the nearest place on the ground or on top of another object already placed… I seem to be babbling now so if anyone has ideas please share with me.

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
1
Best Answer

Answer by HappyMoo · Jan 02, 2014 at 03:10 AM

No problem. You need some basic understanding how unity works.

It's best if you do a tutorial like this one: http://www.youtube.com/playlist?list=PLX2vGYjWbI0RibPF7vixmr4x8ONJX-mNd

Also check, there's a link to the accompanying tutorial website and the downloads of the needed asset package from the asset store in the description of the first video in that playlist.

Comment
Add comment · Show 9 · 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 Carbongrip · Jan 02, 2014 at 03:17 AM 0
Share

Ok why is it that every time I ask something I get a response like this like I have never done anything in unity in my life. I was thinking about this idea and know I need to instantiate the object when I click and I know how to click also if you think that I don't know that. So respectfully was asking if anyone has a idea on how to go about this and would like to make some rough code to help me thanks.

avatar image HappyMoo · Jan 02, 2014 at 03:40 AM 0
Share

It's how you ask looks like you haven't done anything yet. Saying you want to make a huge game and then asking the most basic stuff. It's not like you put your question in any context of what you already have etc.

But I wasn't trying to belittle you - I was just trying to help you in giving you the right information for what seemed to be your knowledge level.

"Follow the mouse around" is also not very clear. Do you want the item to move around the screen at mouse position? in the air or on the ground? In what distance from the camera?

Anyway, placing an object where you click can be achieved by raycasting from the camera and instantiating your prefab at the hitpoint.

Here's how you create the ray: http://docs.unity3d.com/Documentation/ScriptReference/Camera.ScreenPointToRay.html

Then use this ray to raycast it: http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html (It's the last invocation pattern with the ray and the RaycastHit object)

Also make sure the floor and the objects if they should stack have colliders on them, or your ray will go right through them

Let me know if you need more help.

avatar image Carbongrip · Jan 02, 2014 at 07:19 AM 0
Share

Ok got it work but now how can I make it visible at the location of the mouse and as I move the mouse it moves. This way I can preview what it would look like then click to keep one at that position… anything you could do to help would be great…

 using UnityEngine;
 using System.Collections;
 
 public class Placement : $$anonymous$$onoBehaviour
 {
     public GameObject block;
 
     void Start ()
     {
     
     }
     
     void Update ()
     {
         if (Input.GetButtonDown("Fire1"))
         {
              Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             if (Physics.Raycast(ray, out hit, 13))
             {
                    Instantiate(block, hit.point, Quaternion.identity);
             }   
         }
     }
 }
 
avatar image HappyMoo · Jan 02, 2014 at 08:58 AM 0
Share

Create a new layer "permanent" (I assume layer 20 for now) for your objects you will raycast against and put your floor in this layer, but your block prefab shouldn't be in it.

Now instantiate the object you want to place from the start:

 private int permanentLayer = 20
 private GameObject nextToPlace;
 void Start()
 {
   nextToPlace = Instantiate(block, Vector3.zero, Quaternion.identity) as GameObject;
   nextToPlace.SetActive(false);
 }

then remove the check for the fire button, so you raycast all the time... and change your Raycast so you only check for the permanentLayer and also move your preview object to the hit position. Then if you click, let go of the placed object and get a new one to move around

 if (Physics.Raycast(ray, out hit, 13, 1<<permanentLayer ))
 {
   nextToPlace.transform.position = hit.point;
   nextToPlace.SetActive(true);
   if (Input.GetButtonDown("Fire1"))
   {
     nextToPlace.mask = permanentLayer; // place it, so we can put things on top
     // get a new one:
     nextToPlace = Instantiate(block, Vector3.zero, Quaternion.identity) as GameObject;
     nextToPlace.SetActive(false);
   }
 }
 else
 {
   nextToPlace.SetActive(false);
 
 }

You can also have a transparent material on it till you place it and take hit.normal for your rotation into account, if you want to place things on walls etc...

There may be syntax errors in the code - I don't try it, so treat it as pseudocode.

avatar image Carbongrip · Jan 02, 2014 at 05:41 PM 0
Share

Ok got it working… also .mask does not exist it's .layer but anyways was thinking is there a way I can make some kind of grid that the objects snap to the closest of that way its easier to line objects up with each other? maybe even if control held snap to grid == false.

$$anonymous$$y idea is that each rectangular way I place will snap in line with each other. Anything that larger will just be based off of a certain number of rectangles wide and long. That way everything can snap together. And if necessary I could hold control to not snap to grid. Is this possible in unity and could this snap be shown before being placed like if you move your mouse out of a grid it snaps to the next? Could I draw the grid on the ground while I have my game in edit mode?

Show more comments

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

19 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

Related Questions

Making Building Placment rotatable 0 Answers

How to make drawable Paths 0 Answers

I need a grid of objects at runtime, but the gameObject is instantiated later 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 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