• 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
Question by hello_everybody · May 17, 2015 at 08:32 PM · c#physicsraycastcollider3d

Raycast and colliders problem. Corners of two colliders

Hello

Unity 3D stealth game:

I have an enemy character (it is called a 'porter' in the game) that uses raycasting to detect collisions. An error occurs when the enemy reaches the corner of two colliders, for example the corner of two walls.

The enemy character will stop at the walls but can glitch and go through the corners particularly if rotating whilst also trying to move forward (as if pressing up arrow and either left or right arrow on keyboard simultaneously).

Other than corners between colliders the ray casting works as intended.

I've had many hours looking around through other answers and the docs but have not seen this particular problem mentioned. Other than some people suggesting to use rigidbody physics instead.

Currently I'm using transform.Translate to move the enemy character.

Note - the reason I'm drawing the ray using -transform.right is due to the setup of the 3d model imported from blender. As mentioned, this works fine except at corners.

Thanks in advance for any help

Here is the raycasting code:

 // FORWARD raycasts
         
         for (int i = 0; i<collisionDivisionsY; i++) {
             
             float x = p.x + c.x + s.x / 2 * dir; //will put ray at left or right based on x direction
             float y = p.y + c.y - s.y / 2 + s.y / (collisionDivisionsY - 1) * i; // bottom, centre and then top most point of collider
             float z = p.z;
             
             ray = new Ray (new Vector3 (x, y, z), -transform.right);
             Debug.DrawRay (ray.origin, ray.direction);  
             
             if (Physics.Raycast (ray, out hit, Mathf.Abs (deltaX) + skin, collisionMask) || Physics.Raycast (ray, out hit, Mathf.Abs (deltaY) + skin, collisionMask) || Physics.Raycast (ray, out hit, Mathf.Abs (deltaZ) + skin, collisionMask)) {
                 
                 Debug.Log ("hit = " + hit.distance);
                 
                 //get distance between porter position and collider
                 float dst = Vector3.Distance (ray.origin, hit.point);
                 
                 
                 //stop porter's lateral movement after coming within skinwidth of collider
                 if (dst > skin) {
                     
                     deltaX = dst * dir - skin * dir;
                 } else {
                     
                     
                     deltaZ = 0;
                 }
                 
                 movementStopped = true;
                 break;
             }
         



Comment

People who like this

0 Show 0
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

  • Sort: 

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

What is wrong about this RaycastHit Event??? 1 Answer

Cube clips and falls through wall when held 0 Answers

Raycast2D Layermasks and incomplete docs 1 Answer

Held cube clips through wall 0 Answers

Surface with hole and Raycast - Which collider 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