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 Scrillrock · May 07, 2020 at 10:53 AM · raycastcollision detectionclonecollison

Make an object collide with certain other objects when moved to RaycastHit point?

I'm making a simple building system using Raycasting from the mouse pointer. The object that is to be placed is moved (and cloned on click) to the RaycastHit.point of the ray, but I want it to fully collide with objects of its own type, shifting its position relative to he hit point. The object can intersect the terrain as shown in the gif, but should not be inside of the already placed wall blocks. I tried using hit.distance but couldn't figure the detection out, since the object is being positioned at the collision point already. Should I try and detect how should the position should shift relative to the hit point or should I somehow make the MeshCollider work while it's being constantly moved to the RaycastHit.point? alt text

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

Answer by ADiSiN · May 07, 2020 at 02:19 PM

Hi!

If I understood you correctly then I would prefer shifting position solution. As I can see from the gif your pivot point GameObject is somewhat y = 0 and x = 0.5 related to the mesh size. To make sure you are not placing this object inside other already placed GameObject you will need offset it's position. To make it's only for the same type of object (so you can intersect terrain, but not already placed wall blocks) you'll need to detect them. Therefore I would suggest you to try and do this:

  • When receive RaycastHit info check for CompareTag to make sure it's wall block;

  • Calculate the normal to understand at what side you are pointing (this is important, becase, if you are placing on top of object and because your y pivot of GameObject is 0 then you don't need to shift it, right? But along horizontal you will need to always shift to 0.5 of mesh size position, however if you are pointing at down side of GameObject and since your y pivot is 0 then you should shift 1 of mesh size. Keep in mind that it will depend on pivot and currently I am talking about your gif). If you are not familiar with normals then here is good explanation of what normal is: https://answers.unity.com/questions/478017/what-is-a-raycasthit-normal.html

  • Based on the normal direction, multiply your calculated normal to appropriate size of mesh, for example based on gif if it's horizontal it would be 0.5 of mesh size, if vertical UP direction then no shift need and if vertical DOWN then 1 of mesh size;

  • Add calculated shift to hit.point and place give that info to transform position.

What I would like to add - look at the imagealt text There is situation where you are placing your wall and shifting properly, but since there is already placed wall somewhere they could intersect. To avoid that you can simply, before placing visualization of future wall position, cast from place point raycast to 5 directions size of mesh to make sure there isn't any other collider intersections, and shift accordingly or display that there no way to place wall. Keep in mind that this is only a clue of workaround since it may be achieved differently.

Hope that will help and give overall hints where to go further.


ex.png (6.0 kB)
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

175 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

Related Questions

Raycasting not working on a procedural mesh with a mesh collider. 1 Answer

Gun script not working in 5.6 0 Answers

How to draw a ray starting from origin at given azimuth and elevation angles w.r.t. origin? 0 Answers

How to constantly Raycast (V2) 1 Answer

get collider component by raycast 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