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
-1
Question by multinfs · Mar 08, 2014 at 07:30 PM · collisionraycastboxplacement

Placing a cube at the point you look at, without overlapping.

I want to place a cube a certain distance form the player and at a point defined by a raycast going from the camera.

I do a raycast that is rotated so it is following the camera's rotation and I create a simple cube at the hit point. Now the cube is created but since its origin is in the center it is half sunk into the object it was created on.

I need it to be placed on the object I'm facing and not sunk into it. I am not sure how I'd do this since the hit normal doesn't really fix the problem. Any ideas?

Please ask if you don't understand the question.

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 robertbu · Mar 08, 2014 at 10:32 PM

It sounds like you are most of the way there. Typically you use the hit.normal to move the point up. Something like:

   var pos = hit.point + hit.normal * halfObjectHeight;

...where 'halfObjectHeight' is the distance from the bottom of the object you are placing to its pivot point. The value for a default cube will be 0.5 if not scaled. If it is scaled it will be 0.5 * transform.localScale.y;

Comment
Add comment · Show 3 · 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 multinfs · Mar 09, 2014 at 09:57 AM 0
Share

I did try something like that and it works fine but that does still not prevent the cube from overlapping other objects if you try to place it in a corner.

avatar image robertbu · Mar 09, 2014 at 02:56 PM 0
Share

The general solution (arbitrary mesh within and among other arbitrary meshes that moves itself to fit if it can) would be a very complex problem. So the question becomes what (if any) simplifications you are willing to live with. And that is completely based on the nature of your game and the game environment.

One common solution is to not allow the user to place a cube if it hits anything (except the floor). There is no perfect solution for figuring out if the object hits another. Often Physics.SphereCheck() or Physics.OverlapSphere() is used. This will produce false positive hits, but it is very easy to do and can assure that nothing collides.

Another approach might be to use an invisible object with a Rigidbodey, wait a frame after placement to collect the collision information to deter$$anonymous$$e if placement is valid.

If your geometry is simple, you can add some additional Raycasts to move the object away from floors and other object. But this solution has to have a fallback since there are unsolvable problem (like trying to place an object between two other objects where there is just not enough room).

avatar image multinfs · Jun 17, 2014 at 06:14 PM 0
Share

I'm a bit late out with accepting heh, I was just thinking about if I could make a invisible collider to the object and try to detect collisions when I try to place a object but I haven't got around to actually testing that solution. As you said it seems quite difficult to place an object without collisions, but I'll try some of your suggested solutions. Thanks for your answer though!

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

20 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

Related Questions

Unity collision detection only for raycasts 1 Answer

Object jumps through roof when holding "Up" button? 1 Answer

Detect if ball is on a platform or if it fell 4 Answers

Raycast sometimes ignore objects. 1 Answer

Play Audio-Clip On Ray Casting Collision 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