r/UnityHelp • u/Lara372007 • Aug 11 '22
Need help with a box collider issue.
I am new to unity and I'm trying to make a runner game. At first I used a normal cube as my player and the box collider worked perfectly fine. But when I imported a player asset online I began having issues with the box collider.
This is a part of my code:
void Update()
{
if(Input.GetKeyDown(KeyCode.Space) && onGround == true && gameManager.gameIsActive == true)
{
playerRb.AddForce(Vector3.up * jumpForce * Time.deltaTime, ForceMode.Impulse );
playerAnim.SetTrigger("Jump");
onGround = false;
}
}
private void OnCollisionEnter(Collision other)
{
if(other.gameObject.CompareTag("Ground"))
{
onGround = true;
}
}
The problem is that the onGround bool is always set to false even when my player is on the ground. I tried resizing the box collider and I tried using a mesh collider but nothing worked. Any idea of how to fix this?
1
u/MischiefMayhemGames Aug 11 '22
Well I do not have a for sure answer as to what it might be. But I can suggest a few things you can try.
Sometimes importing stuff can screw up tags. So check to make sure the tags are still what they are supposed to be.
You can try adding some debug calls to check to see if the collision is actually occurring, this won't actually solve the problem, but it might help narrow down the cause.
Similarly a collision involves two parties. So it might be an issue with the floor object instead of the player. Again might not be the problem but helps eliminate possible causes.
The script looks like it should work so it does suggest some kind of issue with the collider parameters themselves.