Cannot Jump 3D Object

Hi everyone, as you can guess im Beginner to coding and Unity. I merged few codes but I couldnt figure out how to solve jumping. Could some one give me a hand?

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class CC : MonoBehaviour
{

public float speed;
public Text countText;
public Text winText;
//
public Vector3 jump;
public float jumpForce = 2.0f;

public bool isGrounded;
//
private Rigidbody rb;
private int count;

void Start()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText();
winText.text = "";
//
jump = new Vector3(0.0f, 2.0f, 0.0f);
//
}
void OnCollisionStay()
{
isGrounded = true;
}



void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);
//
{
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
if (transform.position.y <= 1.05f)
{
rb.AddForce(jump * jumpForce, ForceMode.Impulse);
isGrounded = false;
}
}
}
//
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Capsule"))
{
other.gameObject.SetActive(false);
count = count + 1;
SetCountText();
}
}
void SetCountText()
{
countText.text = "Count: " + count.ToString();
if (count >= 3)
{
winText.text = "You Win!";
}
}
}

What exactly is not working? Your code is a bit confusing, which is probably due to you being a beginner, so i’ll list of few things that come to my mind that may be causing problems, or are unconventional.

  • Using an isGrounded variable is fine, however you should only set it true or false at specific moments in time. Currently you are setting it true OnCollisionStay, which means it happens multiple times. This also means that after jumping, you may end up in a situation where you set isGrounded = false, but are still in the collider thus it sets isGrounded = true next frame, enabling you to “double jump”. Instead simply set it true once in OnCollisionEnter. You also may want to specify which collision you mean, such that you dont reset your isGrounded when colliding with, for example, an enemy. You can do this using layers and checking for the layer on collision. (Edit: Is that even a function that properly gets called by Unity? OnCollisionStay should expect a parameter! Unity - Scripting API: MonoBehaviour.OnCollisionStay(Collision))
  • You are already checking for jump inputs and isGrounded, however below that you are again checking if your position.y is below 1.05. First and foremost, this seems like an arbitrary number, secondly isnt that the same check as isGrounded? It seems a bit unnecessary and more like a bandaid to some other problem.
  • You are checking for inputs in FixedUpdate(), which should be avoided. FixedUpdate runs at, well, a fixed update rate. This means it can be called multiple times per frame, or, more likely, only once every few frames, depending on the framerate. Checking for inputs in there is bad for several reasons. One of which is that you can miss inputs. The other and most important reason is that you shouldnt do anything in FixedUpdate that is not physics related. Since FixedUpdate is called at a fixed timestep, slowing down FixedUpdate can cause an exponential slowdown for your entire game. Instead, make moveHorizontal and moeVertical, or just movement, a class variable, which you set in Update each frame, and then simply use the movement vector in FixedUpdate. The same goes for things like GetKeyDown.
  • Your “jump” vector is kind of unnecessary. Unity has a shortcut for (0,1,0), which is called Vector3.up. A scaling of this should happen through your jumpforce, instead of arbitrarily having a (0,2,0) vector defined as upwards and then scaling that through your jumpforce.
  • Your indentations are messed up, making the code harder to read. But i believe that’s just a copy paste problem and not how it actually looks in your IDE.

@Yoreki thank you for your reply. So I already have a working ‘‘jump’’ script. And I have another movement script. But I couldnt merge these 2 scripts. I was thinking like since Y vector scripted as 0.0f it disables jumping. Or it is because of they are at different methods?

jump = new Vector3(0.0f, 2.0f, 0.0f);
    }

    void OnCollisionStay()
    {
        isGrounded = true;
    }

    void Update(){
        if(Input.GetKeyDown(KeyCode.Space) && isGrounded){
            if (transform.position.y <= 1.05f) {
            rb.AddForce(jump * jumpForce, ForceMode.Impulse);
            isGrounded = false;
        }
        }
    }
}
{
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");

        Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

        rb.AddForce(movement * speed);
    }

Since you use AddForce, adding 0 at y should not change anything, or overwrite whatever else you add. Start by placing some Debug.Log lines through your code to see which code sections are getting entered and whether or not the lines for adding force are actually being run.