Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
4 captures
13 Jun 22 - 14 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 vuqarahim · May 16 at 10:54 AM · instantiateraycasthithit.point

Cube instantiaes on wrong side of the plane

I am spawning cube on a plane by clicking and getting the hit.point from Raycast. But sometimes cube spawn on the other side of the planealt text

I am trying to make the cubes snap into positin an not overlap by dividing the plane into Grid Cells and then converting the grid coordinates to World Position, and then spawn the cube on this position.

Instantiating:

   int x, y, z;
                 grid.GetCOORD(hit.point, out x, out y, out z);
 
                 Instantiate(cube, grid.GetWorldPosition(x, y, z) + Vector3.one * grid.CellSize * 0.5f, Quaternion.identity);

This is the conversion methods:

     public Vector3 GetWorldPosition(int x, int y, int z)
     {
         return new Vector3(x, y, z) * cellSize + pivot.position;
     }

    public void GetCOORD(Vector3 worldPosition, out int x, out int y, out int z)
     {
         x = Mathf.FloorToInt((worldPosition - pivot.position).x / cellSize);
         y = Mathf.FloorToInt((worldPosition - pivot.position).y / cellSize);
         z = Mathf.FloorToInt((worldPosition - pivot.position).z / cellSize);
     }
 

Everything works fine on the floor plane, but walls dont behave correctly.

Any help is appreciated, thanks.

image-2022-05-16-144436036.png (84.9 kB)
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 jackmw94 · May 16 at 11:40 AM

I expect this is caused by some floating point error, I would expect this behaviour to happen if my completely flat wall plane was right on the boundary between the two cells.

I'd recommend testing whether this is the case as it is at this point just an assumption about what's going wrong. If this is the case then I'd expect to see the value that you floor inside your GetCOORD function to be very close to an integer before flooring. If this doesn't happen then my hypothesis is wrong and you can ignore the rest of the answer!

Explanation: for simplicity let's say your cell size is 1 and pivot position is (0,0,0). If your wall is at (0,0,2) and facing the z-direction then when you click it you might get a hit location of (-1.23123, 2.82391, 2.0001) or (-1.23123, 2.82391, 1.999998). As you can see the results' z-positions are very close to 2 but not exactly 2. This is called floating point error and exists because computers won't represent floating point numbers (numbers with a fractional component) exactly.


There are a number of ways you can mitigate this and which you choose depends on how you plan to create your levels in the future:

If you decide that walls will always exist on these boundaries then you could add your hit's normal vector to your hit point to get a position that's inside the cell. You'd have to scale the normal so it's smaller than your cell size.

 grid.GetCOORD(hit.point + hit.normal * grid.CellSize * 0.5f, out x, out y, out z);


Alternatively you could use very thin cubes instead of planes to represent the walls, making sure that the sides of the wall that your raycast would hit would be clearly on one side of the boundary rather than ambiguously close to it.

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 vuqarahim · May 16 at 12:13 PM 0
Share

Thanks a lot, it was the floating point error, adding the hit.normal fixed it.

avatar image jackmw94 vuqarahim · May 16 at 12:22 PM 0
Share

Glad to hear! Just a note that you might have to review this if you ever add walls that are diagonal.

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

163 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

Related Questions

Raycast + Instantiate 2 Answers

How to Instantiate GameObject on top of collider? (C# 1 Answer

Raycasting completely cancelling instantiation. 1 Answer

Is it possible to adjust an Objects pivot without affecting any of the child objects? 1 Answer

Position before the camera? 2 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