• 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, s$$anonymous$$fting its position relative to he $$anonymous$$t 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 $$anonymous$$t.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 s$$anonymous$$ft relative to the $$anonymous$$t point or should I somehow make the MeshCollider work w$$anonymous$$le 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 s$$anonymous$$fting 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 t$$anonymous$$s 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 t$$anonymous$$s:

  • 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 (t$$anonymous$$s 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 s$$anonymous$$ft it, right? But along horizontal you will need to always s$$anonymous$$ft 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 s$$anonymous$$ft 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-raycast$$anonymous$$t-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 s$$anonymous$$ft need and if vertical DOWN then 1 of mesh size;

  • Add calculated s$$anonymous$$ft to $$anonymous$$t.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 s$$anonymous$$fting 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 s$$anonymous$$ft accordingly or display that there no way to place wall. Keep in mind that t$$anonymous$$s is only a clue of workaround since it may be ac$$anonymous$$eved differently.

Hope that will help and give overall $$anonymous$$nts 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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 in 2D to prevent a player from moving through walls? 1 Answer

Raycasting to detect which side of collider is hit 2 Answers

How can I let the ray to the edge of the capsule? 1 Answer

Advice for Detecting Intersections With Object Being Placed 1 Answer

Prevent explosion through walls 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