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 NutellaDaddy · May 05, 2014 at 02:17 AM · c#instantiatebuildingplacement

How can I make this move in 3d space?[BUILDING SYSTEM]

I'm trying to make a building system ,but I have no idea where to go with it or how to begin. I want it so that you can move a object that you instantiate and it's distance from the camera will always be the distance between the camera and this empty gameobject(z axis distance) I have. And it should follow your mouse position when it comes to x and y coordinates ,but when you move your mouse in front of a gameobject that intercepts that distance than it will move in front of that. I'm trying to gain something like Kerbal Space Program's way of building. Any tips or tutorials you can point me to?

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 ChristianLinnell · May 05, 2014 at 03:00 AM

It's not as hard as you think.

The following code should go into your Update() method. It does exactly what you ask for.

Make sure the object you've instantiated is on the Ignore Raycast layer! If you don't, it'll come flying towards the camera.

Your next question will probably be "how can I get objects to snap to the right place?". Well that's another question, but the easiest way to do it is to create "snap points" on objects, and then add some logic that lets them figure out if they're near an already-placed snap point :-).

 //The distance the object should be from the camera if it's not "in front" of anything
 float defaultDistance = 5;

 //Make a raycast from the mouse position
 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 RaycastHit hit;

 Vector3 pos;

 //If the ray hits something, get the position of the hit
 if (Physics.Raycast(ray, out hit, defaultDistance)){
     pos = hit.point;

 }else{
     //If it doesn't hit anything, the Ray object will let you get the point it would have hit at a specified distance
     pos = ray.GetPoint(defaultDistance);

 }

 //Set the position
 transform.position = pos;
Comment
Add comment · Show 2 · 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 NutellaDaddy · May 05, 2014 at 03:09 AM 0
Share

Thank you and yes I was already setting up the script for the snap points. Thank you again!

avatar image ChristianLinnell · May 05, 2014 at 03:11 AM 0
Share

All good! Don't forget to accept the answer so others can see it's solved!

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

22 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

Related Questions

RTS Building Structures - How to have building follow my cursor until I click and place it on the grounds? 2 Answers

Game works when i use "build and run" and in the game tab, but not when i build it as an EXE 0 Answers

RTS building placement collision problem C# 1 Answer

Lego brick placement 0 Answers

Unity 2D tile based building simulation(Building problems) 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