• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Even-odd test with raycasting 1 Answer

RaycastHit returns object without collider, in wrong layer 1 Answer

Raycast not hitting collider 2 Answers

Raycast exit point of collider 0 Answers

Raycasting to make Buildings Clickable 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges