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 NechyporukEvgen · Dec 10, 2015 at 11:35 PM · raycastcollidercolliderscollision detectionraycasting

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

Hey. How can I let the ray to the edge of the capsule without use layers? To detect the player as he does not half come from behind the wall, and once out of the wall will be the edge of his collider. An example of the desired result in the picture. Thank you alt text

fghfgh.png (5.1 kB)
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
0
Best Answer

Answer by dhore · Dec 11, 2015 at 01:52 PM

When a ray to the player's position hits a wall you could then do a second check with a ray to each side of the player - though you will need a little bit of math to calculate the sides. Note: We're just going to forget about the y position of the objects and think of it as a top down like your image above.

 Vector2 v = new Vector2(rayPos.x, rayPos.z) - new Vector2(playerPos.x, playerPos.z);
 float r = player.GetComponent<CapsuleCollider>().radius;
 
 Vector2 pos = new Vector2(-v.y, v.x) / Mathf.Sqrt(Mathf.Pow(v.x, 2) + Mathf.Pow(v.y, 2));
 
 // convert back to Vector3 for use with your raycasting
 // (Note: the " *r " on the end is part of the calculation, not the conversion)
 Vector3 side1 = new Vector3(pos.x, playerPos.y, pos.y) * r;
 Vector3 side2 = new Vector3(pos.x, playerPos.y, pos.y) * -r;

Then just do a raycast to points Side1 and Side2. Hopefully that solves your issue.

Comment
Add comment · Show 1 · 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
avatar image NechyporukEvgen · Dec 11, 2015 at 02:09 PM 1
Share

Yes, it's a very good solution to the problem. I thought about it, and started to do. direction of the beam to the left-right-above-below using Vector3.Сross changed. Thanks for the answer, and mathematics. Now I'm sure it will help me

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Raycast exit point of collider 0 Answers

Even-odd test with raycasting 1 Answer

RaycastHit returns object without collider, in wrong layer 1 Answer

Raycast not hitting collider 2 Answers

OnCollisionEnter2D is not working 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