Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 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 Dude_Loe · Feb 07 at 08:13 PM · raycasting

[SOLVED] Place object on top of surface without intersection (or gaps)

(SOLUTION(Using answer from @MerryLeo )): The problem was all along me and my stupid way of writing code. It was all executed in the wrong order and I completely forgot that the closest point on the collider would change as I moved it to the position I wanted. This inevitably created an infinite loop of moving it to the desired place, the distance between the objects changing (moving it away again) and doing the same over and over.
What I did to solve this was simply putting a child object inside the object I wanted to place down, keeping the collider of the parent object, but removing the visual mesh. Then I simply used the closest point on the parent object collider, but only moved the child object (with the visual mesh) to the desired location.
This is probably not a very good solution but it worked for me :)

I've been struggling with this problem for quite some time and with my lack of searching skills I haven't been able to find anything suitable for my case.
What I am trying to achieve is a script that lets me place objects just outside the surface of the object I am pointing the camera at.
The problem right now is that when I do this the object I am placing down is always placed inside the object I am looking at.
This obviously happens because when I set the position of the object I want to place down to the position of the RaycastHit, it uses the position of the origin. The origin of the object I want to place down is always in the middle of it, so it's always inside the object I am looking at with about half of its mass.
How could I make it so no matter size, rotation and shape, the object I want to place down is just outside the surface of the object I am looking at?

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

Answer by MerryLeo · Feb 07 at 09:20 PM

Hello @Dude_Loe ,

RaycastHit has two useful propreties that you can use to achieve your goal: RaycastHit.point and RaycastHit.normal. The first proprety gives you the point of impact in World Space on the mesh and the second gives a vector perpendicular to the surface hit. You can use both of these propreties the following way:

1. You get the RaycastHit.point

2. You add the RaycastHit.normal multiplied by some distance

Note that the distance here is the distance between the surface of the object hit and the center of the object you want to place. I think an easy way to find the distance needed to place the object on the surface would involve using Collider.ClosestPoint(Vector3 position). Here is some sloppy code that might work for you:

 const float distanceFromSurface = 0.1f;
     public Vector3 GetPositionOnSurface(RaycastHit hitInfo, GameObject objectToPlace) 
     {
         Collider objectToPlaceCollider = objectToPlace.GetComponent<Collider>();
 
         // Distance between the center of the object you want to place and the point its surface
         float distFromCenter = Vector3.Distance(objectToPlaceCollider.ClosestPoint(hitInfo.point), objectToPlace.transform.position);
 
         // Total Distance between the center of the object and the surface of the object you want to place your object on
         float totalDistance = distanceFromSurface + distFromCenter; 
         return hitInfo.point + hitInfo.normal * totalDistance;
     }



I hope my explaination was clear.

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 Dude_Loe · Feb 08 at 07:47 AM 0
Share

Thank you a whole lot for this explanation (very clear), but I've already tried something similar...
The problem, however, is that the "objectToPlaceCollider.ClosestPoint(hitInfo.point)" only gives the position of the hitInfo.point.
This is probably just a mistake from my side, but I can't figure out what I'm doing wrong. Any suggestions would be appreciated :)

avatar image MerryLeo Dude_Loe · Feb 08 at 02:21 PM 0
Share

This might be a pointless answer, but are you sure that you are calling the ClosestPoint method on the right Collider? You should call this function on the collider of the object that you are moving. For example, if you are placing a monkey on top of a sphere, the monkey collider.ClosestPoint should be called. If this is not the answer, I will check later

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

137 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

Related Questions

how can i instantiate a GameObject in mouse position and in a special distance from the camera? 2 Answers

Help with Raycast C# 0 Answers

Obstacle avoidance 1 Answer

C# RayCast Fire Cooldown DeltaTime Help 1 Answer

Ultimate FPS Camera, Raycasting Troubles :( 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