- Home /
OnCollisionExit problem
I have two cubes (player and Block) that collide with each other. When the "Player" cube collides with "block" it can jump. After it is skipped I would like to destroy the "Block" cube. This is my current code:
void GetInput()
{
if (Input.GetMouseButtonDown(0))
{
if (currentState == PlayerState.Stading)
{
Jump();
}
else if (currentState == PlayerState.Jumping)
{
StartCoroutine(Smash());
}
}
}
void Jump()
{
rb.AddForce(0, jumpSpeed, 0);
currentState = PlayerState.Jumping;
bc.enabled = false;
}
IEnumerator Smash()
{
currentState = PlayerState.Falling;
rb.isKinematic = true;
rb.velocity = new Vector3(0, 0);
yield return new WaitForSeconds(0.5f);
rb.isKinematic = false;
rb.velocity = new Vector3(0, -smashSpeed);
bc.enabled = true;
yield break;
}
void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Block" && currentState == PlayerState.Falling)
{
currentState = PlayerState.Stading;
rb.velocity = new Vector3(0, 0);
Debug.Log(currentState);
}
}
void OnCollisionExit(Collision other)
{
Destroy(other.gameObject, 0.1f);
}
The collision is successful, but the code in "OnCollisionExit" does not work properly. Can you tell me where I'm wrong? Thanks!
Answer by Vega4Life · Nov 26, 2018 at 09:01 PM
I think you are disabling the box collider when you jump. Isn't that what "bc" is?
Yes: bc = GetComponent(); When player jump, I disable the BoxCollider because the Player can pass through the other cubes.
That is the problem I would assume. You aren't going to get an OnCollisionExit() if it's disabled.
$$anonymous$$aybe for now, move the bc.enable = false; into the OnCollisionExit method. Should get you working somewhat.
Your answer
Follow this Question
Related Questions
GJK box problem 0 Answers
Multiple Cars not working 1 Answer
Collision not working... [Solved (b/c I'm dumb)] 1 Answer
Turn off script of object clones 2 Answers