- Home /
Physics.CheckSphere not working?
I'm trying to use Physics.CheckSphere to ground my character. Problem is, it's always set to true, and I don't know why. It did work at one point, so its not impossible, but I must have changed something and forgot, because now this is happening.
Related script:
public CharacterController controller;
public Animator anim;
public CapsuleCollider groundChecker;
public bool isGrounded;
public float vertVel;
public float gravity;
public float jumpForce;
private Vector3 moveVector;
private AnimatorStateInfo currentBaseState;
void Start()
{
anim = GetComponent<Animator>();
controller = GetComponent<CharacterController>();
}
void FixedUpdate()
{
InputMagnitude();
GroundCheck();
if (isGrounded)
{
vertVel = -gravity * Time.deltaTime;
if (Input.GetAxisRaw("JumpKeys") != 0)
{
vertVel = jumpForce;
Debug.Log("I'm Jumping");
}
}
else
{
vertVel -= gravity * Time.deltaTime;
}
moveVector = new Vector3(0, vertVel, 0);
controller.Move(moveVector);
}
void GroundCheck()
{
float radius = groundChecker.radius;
Vector3 pos = transform.position + Vector3.up * (radius * 0.9f);
//returns true if the sphere touches something on that layer
isGrounded = Physics.CheckSphere(pos, radius);
//Debug.Log(radius);
//Debug.Log(pos);
Debug.Log(Physics.CheckSphere(pos, radius));
}
There are a few other parts of the script (the start, the part that handles player movement), but they're unrelated, and they wouldn't go into the box for script in the post (first time using the forum, don't know what it's called, sorry).
I've ran Debug.Log on the individual components of CheckSphere - they work fine. I put the groundChecker capsule collider as a child of the player, so that the character controller collider doesn't interfere with it. I've tried setting the capsule collider to a trigger and back. I don't know what to do.
Answer by tcz8 · Aug 12, 2019 at 11:55 PM
I know this is an old Post but... FYI The character controller generates its own Collider.
Yes, to be more precise the CharacterController component IS a Collider since the class is derived from Collider. It's essentially an up-right capsule collider. The key to solve this issue is to use a layermask and only check against layers you wish to detect. The easiest way is to declare a public variable:
public Layer$$anonymous$$ask groundLayers;
and select all layers you want to detect in the inspector. The CheckSphere call should look something like that:
isGrounded = Physics.CheckSphere(pos, radius, groundLayers.value);
I aprove! To many posts recommend usign bit shifting. Do yourself a favor and use public layer masks. You will save time being sure you're mask value is right the first time AND down the line, when you need to optimize, you'll be happy you didn't hardcode values.
Answer by ProNoob2 · Dec 11, 2018 at 11:12 AM
Check if some collider is overlapping the sphere. Do it by disabling every collider possibly in the range.
Didn't work, sorry - it still tells me that the player is grounded.
Is it definitely a problem with the project settings?
Yeah he might be right, check your character hierarchy, make sure no colliders are triggering your sphereCheck. And isn't on a detectable layer.
Answer by hanisherif1997_unity · Jul 21, 2021 at 05:45 AM
Hey. Probably I am late, but this might help anyone who is facing this issue. Watch the video below (go to time stamp: 17:34). The ground check is implemented through layermask(so that the checkshphere don not collide with the player and set it always true) and checkshpere.