Question by
zombie2870 · Jun 16, 2020 at 09:28 PM ·
booleanjumping objectgrounded
Collision Check stays on for 2 Space presses rather then 1
Hi All, Im just a little confused I have a Boolean known as isGrounded
Which is in a OnCollisionStay
argument so when the Rigidbody is touching something it will stay on. When I press Space the Ball will jump by using an Impluse force and it will also set isGrounded
to False how ever for some reason it is requiring 2 inputs on space to make this boolean set to false. Would apprentice some help Please see the code below.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
// Jump values
public Vector3 jump;
public float jumpForce = 2f;
// Left right movement values
public Vector3 leftForce;
public Vector3 rightForce;
// Check if player is touching the ground
public bool isGrounded;
//to interact with rigidbody
Rigidbody rb;
private void Start()
{
rb = GetComponent<Rigidbody>();
//Set Jump value
jump = new Vector3(0f, 2f, 0f);
//set Left and right value
leftForce = new Vector3(-0.5f, 0f, 0f);
rightForce = new Vector3(0.5f, 0f, 0f);
}
private void OnCollisionStay(Collision collision)
{
//Check if is grounded
isGrounded = true;
}
// Update is called once per frame
void Update()
{
//Move Left
if (Input.GetKey(KeyCode.A))
{
rb.AddForce(leftForce, ForceMode.Acceleration);
}
//Move Right
if (Input.GetKey(KeyCode.D))
{
rb.AddForce(rightForce, ForceMode.Acceleration);
}
//Jump
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
rb.AddForce(jump * jumpForce, ForceMode.Impulse);
isGrounded = false;
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613001125im_/https://answers.unity.com/themes/thub/images/avi.jpg)