My object does not respond to else.
Hey guys. I am making a 3d platforming game. You move forward all the time and I wanting to make it so that if you collide with an object with the tag ground, you can jump. This is not working and I can jump all the time. I feel like I made a banal mistake, just keep in mind I am a beginner. Thanks.
enter using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class movement : MonoBehaviour
{
public Rigidbody player;
public bool OnGround;
void Start()
{
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Obstacle"))
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
if (collision.gameObject.tag == "Ground")
{
OnGround = true;
}
else
{
OnGround = false;
}
}
void Update()
{
if (player.position.y < -1f)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
{
player.AddForce(0, 0, 30);
}
if (Input.GetKey("a"))
{
player.AddForce(-30, 0, 0);
}
if (Input.GetKey("d"))
{
player.AddForce(30, 0, 0);
}
if (OnGround)
{
if (Input.GetKeyDown("w"))
{
player.AddForce(0, 900, -10);
}
}
}
}
The only time you are potentially changing grounded to false is when you collide with an object. You may want to add a grounded=false into the code that you perform to jump so that it only becomes true the next time you collide with an obstacle.
Did this resolve your issue? If so can you mark this as the best answer?
Your answer
![](https://koobas.hobune.stream/wayback/20220612200536im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do get multiple variables from different objects to test if they are true? 1 Answer
what is EOF, found 'else' PLEASE HELP!! 1 Answer
Enabling & Disabling Script via On Click 1 Answer
Add sprint Key 2 Answers
Bool wont return to false 1 Answer