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 Ubernt · Oct 29, 2015 at 08:56 PM · raycastmeshcollidermeshrenderercolliding

How to prevent mesh to not go through other meshes

I use a Raycast to render a mesh where ever I look. However, when it renders any mesh I use, it goes through the other objects.Mesh through wall

Code:

 RaycastHit Hit;
         if (Physics.Raycast(direction.position, direction.forward, out Hit) && destinationPoint != null)
         {
             if (destinationPoint.transform.position != Hit.point)
                 destinationPoint.transform.position = Hit.point;
         }

If I add Mesh Collider, it starts to move and rotate strangely and I'm also as a player moving too. How I can make this work? Doe anyone have a good proper solution to be able to render a mesh without going through other meshes but it can collides?

meshthroughwall.png (49.8 kB)
Comment
Add comment · Show 7
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 meat5000 ♦ · Oct 29, 2015 at 09:00 PM 0
Share

Ins$$anonymous$$d of instantiating at hit.point, add half the object bounds to the distance, normal to the wall.

avatar image Ubernt meat5000 ♦ · Oct 29, 2015 at 09:37 PM 0
Share

Sounds tricky. I have a resource folder where I load objects to the game. So I'm not able to know always where is Pivot point of model. Can you provide a code or something to test it out?

avatar image Statement Ubernt · Oct 29, 2015 at 09:45 PM 0
Share

But you have a renderer, and the renderer has bounds. Yes, it's "tricky", but if you have an arbitrary shape, you need to define some way of defining offset from wall, and that will vary for each object. Either do the maths, or add a special transform game object which you put on each object to define the "root" for wall mounting.

Show more comments
avatar image Firedan1176 · Oct 29, 2015 at 11:56 PM 0
Share

Consider using joints.

avatar image Ubernt · Oct 30, 2015 at 02:44 PM 0
Share

Statement thank you for your information but I have been learning unity for a month so I think I need some code to understand what you are saying. I have been learning from udemy courses and different videos to get the basics.

avatar image Statement Ubernt · Oct 31, 2015 at 11:31 AM 0
Share
 RaycastHit Hit;
 if (Physics.Raycast(direction.position, direction.forward, out Hit) && destinationPoint != null)
 {
     Vector3 offset = GetItemOffset(item, Hit.normal);
     Vector3 position = Hit.point + offset;
     if (destinationPoint.transform.position != position)
         destinationPoint.transform.position = position;
 }
 
 // ...
 
 Vector3 GetOffsetForPrefab(GameObject item, Vector3 normal)
 {
     // Based on that normal, figure out what prefab requires 
     // so you can provide an offset. Up to you how you should solve it.
     // Perhaps you want to base it off renderer bounds? Perhaps you want
     // to solve it with an anchor point? Perhaps you want to solve it 
     // polymorphically with a component that figures it out for you?
     // Perhaps you want to solve it based on colliders? Etc. Etc.
      
     // var renderers = item.GetComponentsInChildren<Renderer>(); // ?
     // var anchor = item.transform.Find("Anchor"); // ?
     // var offsetCalculator = item.GetComponent<OffsetCalculator>(); // ?
     
     // Do work based on method...
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
-1

Answer by _Yash_ · Nov 05, 2015 at 01:35 PM

 public float speed;
 void Update(){
 
     Vector3 target = hit.point; // where object should be
     Vector3 source = trans.position; // where object is right NOW
 
     // object should travel vel(distance) in speed(time) to reach the target
     Vector3 vel = (hit.point - trans.position) * Time.deltaTime * speed; 
 
     rigidbodyOfObject.velocity = vel;
 }  

tweek the speed value to get the good result

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Fill area defined by a contour 1 Answer

Launching a Projectile to a Raycast 1 Answer

Does RaycastHit.textureCoord assume collider mesh and renderer mesh the same? 2 Answers

Detecting how many times a raycast collides with a single concave meshcollider 2 Answers

Getting color from mesh renderer causing crash 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