- Home /
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
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.
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
Follow this Question
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