Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 KKS21199 · Mar 04, 2017 at 07:23 PM · c#raycasttransformposition

Place Gameobject within a specific tranform value 3d

Hi,

I am trying to place a game object wherever the user chooses within the game (3d) but it has to has a specific transform value (0.5, 0, 0.5) (0.5, 0, -0.5) (-0.5, 0, 0.5) (-0.5, 0, -0.5).

I tried generating quads during the start of the game so I could have a template and use OnMouseOver function to decide where to place the gameobject. But the quad's degrade performance as soon as I start increasing the number of quads within the scene (at about 200x200 FPS starts falling gradually and at 500x500 FPS is only about 5-10).

So I decided to lock the position when the user places the gameobject. So if they touch near (0, 0, 0.1), the gameobject should be moved to (0.5, 0, 0.5).

Any ideas on how I could do the logic? The ground would be a mesh and will have a flat surface.

Comment
Add comment · Show 4
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 tinglers · Mar 04, 2017 at 07:25 PM 0
Share

exactly what is the question? how to move gameobjects to move to a grid point?

avatar image KKS21199 tinglers · Mar 04, 2017 at 07:31 PM 0
Share

Yes, $$anonymous$$oving a gameobject to a grid point.

avatar image tinglers KKS21199 · Mar 04, 2017 at 07:41 PM 0
Share

the elegant way is lost from me at this moment, but: float decimal = value - math.floor(value); if (decimal < 0.5) float x = math.floor(value) + 0.5; else float x = math.ceil(value); forgive me for how bad this solution is i shall spend some more time contemplating a better way

Show more comments

3 Replies

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

Answer by KKS21199 · Mar 05, 2017 at 10:58 AM

This script works perfectly. Thanks to @tinglers for the idea. This will lock the gameobject to the grid. Anybody looking for the same solution can use the function below and extend the function to check if the points are unique.

  void placeItem() //placeItem wherever mouse clicked
     {
         Vector3 newPosition;
 
         if (Input.GetMouseButtonDown(0))
         {
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast(ray, out hit))
             {
                 newPosition = hit.point;
 
                 float xPos = Mathf.Abs(newPosition.x - Mathf.Floor(newPosition.x));
                 if (xPos > 0.5f)
                     newPosition.x = Mathf.Floor(newPosition.x) + 0.5f;
                 else
                     newPosition.x = Mathf.Floor(newPosition.x) - 0.5f;
 
                 float zPos = Mathf.Abs(newPosition.z - Mathf.Floor(newPosition.z));
                 if (zPos > 0.5f)
                     newPosition.z = Mathf.Floor(newPosition.z) + 0.5f;
                 else
                     newPosition.z = Mathf.Floor(newPosition.z) - 0.5f;
 
                 newPosition.y = 0.5f;
 
                 transform.position = newPosition;
             }
         }
     }

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
1

Answer by tinglers · Mar 05, 2017 at 09:09 AM

if your planes are 1 big then simply x = math.floor(mouseX) + 0.5 and z = math.floor(mouseZ) + 0.5 should do the trick. This calculation won't necessarily make the value a unique one so for that you'd have to check with the quad on the resulting position.

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 LiloE · Mar 04, 2017 at 10:44 PM

Can you be more specific about the value requirements? Can it be any multiple of 0.5 like -20, 3.5, 12, 75.5 ?

Also, are the x,y,z values of the vector dependent on each other or not?

Comment
Add comment · Show 1 · 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 KKS21199 · Mar 05, 2017 at 05:54 AM 0
Share

It shouldn't be in multiples. It more like the grid so if 0.5, 0, 0.5 is the first grid and the one next to it would be 1.5, 0, 0.5. https://i.gyazo.com/8d72ecec72a107f7d3867486e46cafe5.gif This is the quad grid I generated. So when user clicks I could get the specific quad and then put the game object in its position.

The x, y, z should be unique to each and (+ 1 in transform) on either x or z axis or both. It could start with 0, 0, 0 as well. I started with 0.5 so it aligns with unity's own grid.

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

299 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 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 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 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 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 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 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 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 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 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 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 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 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 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

Check if player object is right above this object 1 Answer

How to make Camera position Independent of its Rotation? 1 Answer

Trying to use transform.forward properly 1 Answer

during build object moves different position than editor(vr) 1 Answer

Player using turret problem 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