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 /
  • Help Room /
avatar image
1
Question by Tonts · Nov 11, 2010 at 01:14 AM · gridbuildingsconstruction

Grid of furniture in game like The Sims

so this is my first question here :D and I need help with a script that can create an invisible grid that attaches the furniture to its space, just like in The Sims, when you are buying the furniture and it can check if there is already a gameobject there. Please Help

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

Answer by Robinmtb · Jan 15, 2012 at 12:35 AM

Hi Tonts!

You can make a grid like that like this:

 public var gridObject : Transform; //The floor-object in your project

 public var gridElementsX = 6; //Number of grid elements in X-axis
 public var gridElementsZ = 4; //Number of grid elements in Z-axis
 
 public var gridElementSizeX = 4; //How long a gridelement's side is in X-axis.
 public var gridElementSizeZ = 2; //How long a gridelement's side is in Z-axis.
 
 private var totalGridSizeX = numberOfGridElementsX * gridElementSizeX;
 private var totalGridSizeZ = numberOfGridElementsZ * gridElementSizeZ;

 private var furniture: Transform; //The furniture you wish to position

 function Update (){
    //Use RayCast wich will store the position the cursor is pointing at in a RaycastHit, providing your floor has a collider!
    var hit : RaycastHit;
    var ray = camera.ScreenPointToRay(Input.mousePosition);
    Physics.Raycast(ray,hit,Mathf.Infinity);
    
    furniture.position.x = gridObject.position.x + Mathf.Floor(hit.point.x * (gridElementsX / totalGridSizeX)) * gridElementSizeX;
    furniture.position.z = gridObject.position.z + Mathf.Floor(hit.point.z * (gridElementsZ / totalGridSizeZ)) * gridElementSizeZ;
 }


That should do the trick to get it to position properly :) Just change the values so they work for you and don't forget that the RayCasting depends on having a collider that "isTrigger" on the floor-object.

I'm sorry if there are any errors in the code. I didn't have time to test it.

Good luck! /Robin

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
avatar image
0

Answer by macdude2 · May 20, 2011 at 04:04 AM

what you can do is either put a trigger on the bottom of the furniture and then set the OnTriggerEnter() function to what ever you want to do, say turn the grid red. Or you can do linecasts or raycasts to check to see if there is anything around your furniture.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Help with registering collisions while snapping an object in-game 2 Answers

Getting World Space From Custom Grid 1 Answer

Positioning Screen Space UI element in center of a grid square? 1 Answer

How To Get Tile Cell Position 0 Answers

ProBuilder Element Snap to Grid (Unity 2020.1.2f1) 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