unity – How do I detect if my object is grounded?

As @DMGregory pointed out, there are soo many tutorials on this subject, but since I have just answered a similar question (https://stackoverflow.com/a/66938817/1405906) I will try to address some parts of your code:

I assume that the collider (bc) is connected to your player. If so, bc.gameObject.name, will always be the name of your player object. That is why isGrounded never is set to true.

if(bc.gameObject.name == "Grid") {
        isGrounded = true;
        NumberJumps = 0;
    }

Physics should be done in FixedUpdate(), so that is good, but you should not try to get the input there. You might miss “one-time” events like a jump since it is not guaranteed that FixedUpdate() will be called in every frame. Move the check to Update() and store the jump flag in a boolean.

bool jump = false;
Update()
{
    if( Input.GetButtonDown("Jump") )
        jump = true;
    ...
}

Since you want to support a double jump (or more), the ground check should only be done before the first jump is launched. Once jumping, the player is in the air and there is no ground – but you still want to be able to do the second jump.

FixedUpdate()
{
    if(jump)
    {
        if( isGrounded || (NumberJumps>0 && NumberJumps<MaxJumps)
        {
            rb.AddForce(Vector2.up * jumpHeight);
            NumberJumps += 1;
        }
        jump = false; // Remember to reset the jump flag.
    }
    controller.Move(horizontalMove * Time.fixedDeltaTime, false, false);
}

Now it should work. For more information on how to do the ground-check, see my answer above, or google. Remember to reset the NumberJumps to 0 as soon as you are grounded. Good luck!

unity – My character can jump even when its not grounded

Here is the code

using System.Collections.Generic;
using UnityEngine;

public class PlayerMovementscript : MonoBehaviour
{
    //vars
    public CharacterController controller;

    public float speed = 12f;

    Vector3 velocity;

    public float gravity = -9.81f;

    public Transform groundCheck;
    public float groundDistanace = 0.4f;
    public LayerMask groundMask;
    public float jumpHeight = 3f;
    bool isGrounded;
    //-----------------------------------


    

    // Update is called once per frame
    void Update()
    {
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistanace, groundMask);

        if(isGrounded && velocity.y < 0)
        {
            velocity.y = -2f;
        }

        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        Vector3 move = transform.right * x + transform.forward * z;

        controller.Move(move * speed * Time.deltaTime);

        if(Input.GetButtonDown("Jump") && isGrounded)
        {
            velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
        }



        velocity.y += gravity * Time.deltaTime;

        controller.Move(velocity * Time.deltaTime);
    }
}```

c# – Checking if player is grounded using tilemap collision detection

beginner here just trying to learn the basics. I was following this tutorial but I ran into some trouble. Heres a screenshot of unity

enter image description here

So the tutorial calls for tagging the terrain with “Grounded” and essentially checks if the 2D collision box for GroundCheck collides with anything with a “Grounded” tag using the Move2D and Grounded scripts below. Note that the Move2D script is a component of the Player gameobject.

Move2D

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Move2D : MonoBehaviour
{
    public float movespeed = 5f; 
    public float jumpforce = 10f;
    public bool isGrounded = false;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        Jump();
     Vector3 movement = new Vector3(Input.GetAxis("Horizontal"),0f, 0f);
     transform.position += movement * Time.deltaTime * movespeed;
    }
    void Jump(){
        if (Input.GetButtonDown("Jump") && isGrounded == true){
        gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, 5f), ForceMode2D.Impulse);
        }
    }
}

Grounded.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Grounded : MonoBehaviour
{
    public GameObject Player; 
    // Start is called before the first frame update
    void Start()
    {
         Player = gameObject.transform.parent.gameObject; 
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if(collision.collider.tag == "Ground")
        {
            Player.GetComponent<Move2D>().isGrounded = true;
            Debug.Log("test");
            print("GROUNDED");
        }
    }

    private void OnCollisionExit2D(Collision2D collision)
    {
        if(collision.collider.tag == "Ground")
        {
            Player.GetComponent<Move2D>().isGrounded = false;
        }
    }
}


The error I’m running into is that it appears unity isn’t recognizing that the player has collided with the tilemap collision box, so the bool isGrounded is just perpetually false and the player is unable to jump. Any help would be appreciated!

Is it fair for my Mom to keep me grounded for something I said months ago? Here’s what happened…?

Four months ago, before the coronavirus thing happened, my Mom and I took a plane trip back to San Diego where we live.

While on the airplane, the flight attendant asked me, “Would you like some nuts?”

I was trying to be funny so I laughed and told her, “No thanks. Would you like mine?”

I was JUST joking. Hell, even the flight attendant smiled and just went about her way.

However, my Mom was super mad and she took away all of my video games and T.V out of my room.

So my sisters get to play video games, but I’m still stuck with nothing.

It’s been 4 months and I told her how sorry I was, but she’s still not budging.

We’re going through a coronavirus thing so I need something to do while I’m home.

Updated 1 day ago:

Got my games back last night lol

Box2d Raycast C ++. Always return wrong? (tries to check if the player is grounded)

I am trying to add Box2d to my C ++ / SFML platform game. I have my floor tiles with a b2staticBody. And the player character is b2_dynamicBody.

My usual way to check if the player is grounded is to broadcast raycast directly from the middle of the sprite to just below the foot level. (NOTE: I notice that SFML is y-down).

Right now I just want the raycast to return true when it hits something. But it always returns false. I watch the player land on the floor and walk and fall over the floor (i.e. the built-in Box2d collision works).

Here is my code (I am brand new to using Box2d in C ++. Sorry if it is wrong. There are not too many good examples that can be found online.)

void guy::update() {
bool bGrounded = false;
b2RayCastOutput output;
b2RayCastInput input;
input.p1 = bodyPtr->GetPosition();
input.p2 = bodyPtr->GetPosition() + b2Vec2(0, 1.f * Constants::PPU); 
input.maxFraction = 1.0f;
b2Fixture* fixture = bodyPtr->GetFixtureList();
std::cout << "p1: " << input.p1.x << " , " << input.p1.y << " / p2: " << input.p2.x << " , " << input.p2.y << std::endl;
std::cout << "sprite pos: " << sprite.getPosition().x << " , " << sprite.getPosition().y << std::endl;
if (fixture->RayCast(&output, input, 0))
{
    bGrounded = true;
}
else
{
    bGrounded = false;
}
std::cout << "isGrounded: " << bGrounded << std::endl;
}

The console outputs appear to be OK (ie p1 and p2 are where I expected them to be), but the bool always returns 0.

Of course I am very open to an improved jumping function. Finally, I would like to add the ability to hold the jump button for a while to get a bigger jump. But the only way I can start is to check that the player hits the ground first.