- Home /
Am I casting the capsule incorrectly?
I'm making a top-down zombie survival game and I want to detect the items around the player which can then be picked up when highlighted.
Physics.CapsuleCast(transform.position + Vector3.down * 3, transform.position + Vector3.up * 3, 3, Vector3.up, out hit, layerMask.value);
I'm trying to use the Physics.CapsuleCast() function to detect one of the items around the player but for some reason CapsuleCast() always returns false and I can't figure out why.
Am I casting the capsule incorrectly?
This is how I want to cast it:
The mask should be correct so the only thing I can think about being incorrect is the casting.
Answer by zee_ola05 · Jan 06, 2017 at 12:59 AM
I think what you want is the Physics.OverlapCapsule. There is also Physics.CheckCapsule if you only wanna know if you're colliding with your target (layerMask).
You would wanna use Physics.CapsuleCast when you wanna know in advance if you're character will be colliding with an object. In this case, you would cast it in the direction your character is facing.
It worked! There are a few drawbacks however. As the return type is an array of colliders, it doesn't return a bool to say whether it detected something or not so my solution was to see if the array's length is longer than 0. Not really a draw back but something to get used to.
Also because it returns colliders I don't get the same functionality that I get from the RaycastHit class such as where I hit the collider and such.
All in all, it works so thank you so much!
Your answer
Follow this Question
Related Questions
Why does CapsuleCast appear in the wrong place? 0 Answers
The CapsuleCast is broken. 0 Answers
add a float and object 1 Answer
float to string c# unity script Android csharp 2 Answers
how to hit a gui button with raycast 2 Answers