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 rageingnonsense · Nov 13, 2015 at 09:52 PM · raycastcapsulecast

Looking for advice to get around issues with CapsuleCast

Consider the following scenario:

I have a meshcollider that is a wavy plane, with a bit of thickness. Imagine a blanket on a badly made bed. I need to trace the edge of this plane to get a list of points, which will later be used.

What I came up with (and works most of the time) is to run a set of consecutive capsule casts (since I do not know the elevation of the side I am casting against yet)), using the normal from the last hit to perform the next cast, until I have traced the desired area. Imagine tapping the edge of the blanket with a broomstick (longways, such that the side of the stick hits the edge of the blanket).

The issue I have with this approach is that occasionally, I hit the TOP of the plane, and not the side (remember, it DOES have thickness, so there is a side to hit for sure). This causes the normal to be facing the wrong direction, and messes up the rest of the trace.

The normal returned from RaycastHit for this is not correct due to the nature of capsulecast and spherecast. UI already account for and correct this by taking the face that was hit, and manually calculating the normal (this is confirmed correct), via this method:

 /// <summary>
     /// Calculates the surface normal of the collider that was HIT.
     /// This is to be used for sphere and capsule casts, where the 
     /// hit normal is actually based on the caster shape, and not the
     /// collider hit
     /// </summary>    
     /// <returns>Calculated Normal</returns>
     public static Vector3 ColliderNormal(this RaycastHit hit, LayerMask mask = default(LayerMask)) {
         Vector3 normal = Vector3.zero;
 
         if(hit.collider is MeshCollider) {            
             Mesh mesh = ((MeshCollider)hit.collider).sharedMesh;
             int[] tris = mesh.triangles;
             Vector3[] verts = mesh.vertices;
  
             Vector3 v0 = verts[tris[hit.triangleIndex * 3 + 0]];
             Vector3 v1 = verts[tris[hit.triangleIndex * 3 + 1]];
             Vector3 v2 = verts[tris[hit.triangleIndex * 3 + 2]];
  
             normal = Vector3.Cross(v1 - v0, v2 - v1).normalized;
  
             //return hit.transform.TransformDirection(n);
 
         } else {            
             Vector3 p = hit.point + hit.normal * 0.01f;
             Physics.Raycast(p, -hit.normal, out hit, 0.011f, mask);
             normal =  hit.normal;
         }
 
         return normal;
     }

This has nothing to do with the issue, I just wanted to post it in case people started suspecting it.

I need to always get the SIDE of the plane, but capsulecast is unreliable. I have tried a few things to derive the side further (like raycasting against the bottom, lerping a new point to raycast against that is between both points), but that is turning out to be unreliable.

So what I wonder is, how would you handle this? What approach would you go about to get hits along the edge? I'm welcome to any and all suggestions. It is worth noting that I am rarely ever tracing the entire edge; I need subsets of the edge (a constraint I enforce with two planes).

I hope this makes sense. If needed, I can try to draw a shitty bmp to illustrate it further.

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

0 Replies

· Add your reply
  • Sort: 

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

32 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

Related Questions

How do i make the object at the end of my raycast not phase into a wall? 2 Answers

Game become slow after CapsuleCast on terrain? 1 Answer

What is wrong with my Capsule cast? 1 Answer

Why is CapsuleCast/Raycast performance effected by rigidbody? 1 Answer

Capsule Cast Direction Help 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