,Jump Mechanic - isGrounded using enter- and exitOnCollision - isGrounded stays false when touching floor object from side.
Hello,
I am trying to implement a proper jump mechanic into my game. In its current state is is somewhat working. I am checking to see if the player collides with a floor object. If it does, it sets isGrounded to true, allowing for the player jump. However, if the player touches a floor object, lets say a cube from the side, it will set isGrounded to false, and never return true again, even though the player is standing on a floor object. How do I solve this? Should I consider changing my jumping logic? If yes - to what? Thank you in advance.
I have taken these screencaps to show the issue visually:
This is my code:
void Update()
{
/* Some horizontal movement code here...*/
//Jumping
if (isGrounded == true)
{
velocityZ = +1;
if (Input.GetKey(KeyCode.G))
{
velocityZ = jumpVelocity;
}
}
}
}
void FixedUpdate()
{
//Horizontal Movement
rb.velocity = new Vector3(velocityX * Time.deltaTime, velocityZ * Time.deltaTime, velocityY * Time.deltaTime);
velocityY = velocityY * (decc);
velocityX = velocityX * (decc);
//Vertical movement
if (transform.position.y > 1) velocityZ = velocityZ - jumpForce;
}
private void OnCollisionExit(Collision other)
{
if (other.gameObject.name == "floorObject" || other.gameObject.name == "mainGround")
{
isGrounded = false;
Debug.Log(isGrounded);
}
}
private void OnCollisionEnter(Collision other)
{
if (other.gameObject.name == "floorObject" || other.gameObject.name == "mainGround")
{
isGrounded = true;
Debug.Log(isGrounded);
}
}
Your answer
Follow this Question
Related Questions
OnCollisionEnter/OnControllerColliderHit/onTriggerEnter Won't work as teleporter? 0 Answers
How to Destroy A GameObject when it enters any Triggers 2 Answers
OnTriggerEnter2D not working 2 Answers
OnTriggerEnter not firing 2 Answers
Trigger not detected 0 Answers