- Home /
How to keep CapsuleCast() from sweeping?
I want to do a CapsuleCast up/down/left/right of my character but I don't need to sweep it in a direction...so do I use Vector3.Zero for that parameter? Can anybody give me an example of how to do this??? Thanks.
Answer by Jessy · Oct 18, 2010 at 03:06 AM
Not sweeping doesn't make any sense; that's what casting is. What don't you understand about the example in the docs? All you'd need to change about it is to switch out transform.forward, and instead use transform.up, -transform.up, -transform.right, and transform.right.
Edit: Here's an "illustration" of what point1 and point2 are, on the capsule with parentheses for endcaps:
point1--->(____)<---point2
The part I don't get is the start and end point??? I would think you would have an origin and then sweep it in any direction. I can set the start and end points to get the result I'm looking for but I just can't wrap my $$anonymous$$d around why you would need to set a start and end point and then sweep it?
You need to define a capsule before you can sweep/cast it. point1 and point2 are just the ends of the capsule. That is, right in the middle of the hemispheres on the ends, on the edge.
Yea I get that but I guess I don't get why you would do that because the points will change the second it sweeps right?
I don't think you do get it; you're asking what they are, when they are essential parts in defining a capsule. You define a capsule, with two points and a radius. Then you send it in a direction. It's basically the same thing as if you had a capsule mesh and used rigidbody.AddForce on it.
Answer by guavaman · Oct 06, 2012 at 05:15 PM
I want to do a CapsuleCast up/down/left/right of my character but I don't need to sweep it in a direction...so do I use Vector3.Zero for that parameter?
It sounds like you might want to use Physics.CheckCapsule http://docs.unity3d.com/Documentation/ScriptReference/Physics.CheckCapsule.html This will tell you if anything collides anywhere inside a stationary capsule volume. You could combine this with a Physics.CapsuleCast to cover both the starting position and a cast in a direction, but based on your usage of scaling, it doesn't sound like you really need a sweep test, just the volume test of Physics.CheckCapsule.
A note about capsule cast:
There is a lot of confusion about how CapsuleCast works because we expect (myself included) CapsuleCast to do something its not designed to. Capsule casting does not just project a solid volume from start to finish and detect any collisions in any direction in its swept volume. Instead, it's like casting a ray. It only hits if the collider it intersects has a face with normal toward the ray origin. This probably answers all the weird cases of "why doesn't capsule cast collide when..."
so I can't tell if I'm getting what I want.
Use gizmos to visualize it. Just make a couple of spheres at the start/end of the capsule and and again at the final position of the cast. http://docs.unity3d.com/Documentation/ScriptReference/Gizmos.DrawSphere.html
Here is a little test script you can pop onto a gameobject that will let you see a capsule and move it around to see where it collides. It updates in the editor. If you put it near a wall, you'll notice it turns red until you move the origin spheres slightly into the wall, then it turns back white (no hit).
using UnityEngine; using System.Collections;
[ExecuteInEditMode] public class Capcasttest : MonoBehaviour { public enum Mode { CapsuleCast = 0, CheckCapsule = 1 };
public Mode mode = Mode.CapsuleCast;
public Vector3 height = new Vector3(0, 2.0f, 0);
public float radius = 1.0f;
public float dist = 3.0f;
private Vector3 vOffset;
private Vector3 dir;
private bool hit;
void Awake() {
vOffset = new Vector3(0, radius, 0);
}
void Update () { dir = transform.forward; Vector3 botSpherePos = transform.position + vOffset; Vector3 topSpherePos = botSpherePos + height;
if(mode == Mode.CapsuleCast) {
hit = Physics.CapsuleCast(botSpherePos, topSpherePos, radius, dir, dist);
// Draw lines
Color lineColor = Color.white;
if(hit) lineColor = Color.red;
Debug.DrawLine(botSpherePos, botSpherePos + (dir * dist), lineColor);
Debug.DrawLine(topSpherePos, topSpherePos + (dir * dist), lineColor);
} else
hit = Physics.CheckCapsule(botSpherePos, topSpherePos, radius);
}
void OnDrawGizmos() {
Vector3 botSpherePos = transform.position + vOffset;
Vector3 topSpherePos = botSpherePos + height;
// Draw spheres
if(hit) Gizmos.color = Color.red;
Gizmos.DrawSphere(botSpherePos, radius); // draw origin top sphere
Gizmos.DrawSphere(topSpherePos, radius); // draw origin bottom sphere
if(mode == Mode.CapsuleCast) {
Gizmos.DrawWireSphere(botSpherePos + (dir * dist), radius); // draw swept bottom sphere
Gizmos.DrawWireSphere(topSpherePos + (dir * dist), radius); // draw swept top sphere
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613080004im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Test stationary capsule against colliders in scene. 2 Answers
Keep Horizontal Momentum after Jump 2 Answers
How to stop sphere from clipping through cube edges aside from lowering Time.FixedDeltaTime? 1 Answer
Collision.impulse = 0 in OnCollisionStay Kinematic Static collision pair after Update 0 Answers