• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By wobbegong_
      I am taking an absolute beginner's game development course and we have just finished game jams in small groups. Our current assignment is to get feedback from people working in any aspect of game development. I would very much appreciate any feedback! The game is up on itchi.io (sound warning) https://wobbegong.itch.io/zombie-shooter It's essentially a very basic PvE.
      I also have some things I'm wondering about, but you don't necessarily have to answer these. 
      1. Do you have any tips on working with physics? My group wrestled a bit with Rigidbody physics not totally working the way we wanted to -- jumping ended up kind of floaty and inclines seem to mess up movement. Alternatively... how can I build terrains with depth that won't result in wonky physics?
      2. How can I keep up the level of challenge in an interesting way as the player progresses through the waves?
      3. What are some of your personal guidelines for creating title screens?
      Thank you very much in advance!
    • By ethancodes
      I'm having a weird issue with detecting a collision. I've tried everything I could find online but nothing seems to work. I have a brick object. It has a 2D Collider attached and I have also attached a 2D Rigidbody on it. I also have an EndScreen 2D Collider. The EndScreen 2D collider is tagged with "EndScreen". I am trying to detect when a brick collides with the end screen collider and simply print "game over" in the console. 
      This is my current code for this part of the program, it is attached to the bricks:
      void OnCollisionEnter (Collision2D collision) { if (collision.gameObject.tag == "EndScreen") { Debug.Log("Game over"); } } Several things have happened depending on the set up. If I have the rigidbody 2D set as static, my ball object can still collide with the bricks, but I get no Log message. If I set it to Kinematic or Dynamic, I get absolutely no interaction between the ball and the bricks, and nothing when the bricks pass through the collider. I have tried to set the collider to a trigger and use OnTriggerEnter2D, no change. I have tried to put the rigidbody on the EndScreen object and tried to set it's body type to all 3 settings, no change. The only thing I can think of that I have not done is put the script on the EndScreen object and switch the tag to the bricks. The reason I have not done this is because I will have several types of bricks, some of which will have different tags. 
       
      Please tell me somebody can see what I'm doing wrong here, because I'm losing my mind over something I feel should be ridiculously simple. Thanks.
    • By Sandman Academy
      Downloadable at:
      https://virva.itch.io/sandman-academy
      https://gamejolt.com/games/sandmanacademy/329088
      https://www.indiexpo.net/en/games/sandman-academy
      https://www.gamefront.com/@sandmanacademy
      http://www.indiedb.com/games/sandman-academy
    • By Sandman Academy
      Downloadable at:
      https://virva.itch.io/sandman-academy
      https://gamejolt.com/games/sandmanacademy/329088
      https://www.indiexpo.net/en/games/sandman-academy
      https://www.gamefront.com/@sandmanacademy
      http://www.indiedb.com/games/sandman-academy
    • By Sandman Academy
      Downloadable at:
      https://virva.itch.io/sandman-academy
      https://gamejolt.com/games/sandmanacademy/329088
      https://www.indiexpo.net/en/games/sandman-academy
      https://www.gamefront.com/@sandmanacademy
      http://www.indiedb.com/games/sandman-academy
  • Advertisement
  • Advertisement
Sign in to follow this  

Unity How To Check If Grounded In 2D Unity Game?

This topic is 865 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey,

I've implemented jumping and moving in my game, but want to prevent infinite jumping. I've tried to integrate a few systems into my game, but have been unable to do so. Can anyone help?

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{

    private float moveSpeed = 5f;
    private float jumpHeight = 300f;
    private Rigidbody2D rigidBody;

    // Use this for initialization
    void Start()
    {
        rigidBody = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f)
        {
            transform.Translate(new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f));
        }

        if (Input.GetKeyDown(KeyCode.W))
        {
            rigidBody.AddForce(new Vector2(0, jumpHeight));
        }
    }
}

EDIT: Found a solution. Create an empty, make it child of player, then place it where the feet are. Here's the code I used. You have to give all ground a layer, and put that layer in this script.

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{
    //Variables for general movement.
    private float moveSpeed = 5f;
    private float jumpHeight = 300f;
    private Rigidbody2D rigidBody;

    //Variables for grounding system.
    public bool isGrounded;
    public Transform grounder;
    public float radius;
    public LayerMask ground;

    // Use this for initialization
    void Start()
    {
        rigidBody = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        //Were keys 'A' or 'D' pushed down?
        if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
        {
            //Move the object to it's current position * moveSpeed * time.deltaTime.
            transform.Translate(new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f));
        }

        isGrounded = Physics2D.OverlapCircle(grounder.transform.position, radius, ground);

        //Was the key 'W' pushed down?
        if (Input.GetKeyDown(KeyCode.W) && isGrounded == true)
        {
            //Add force to the rigid body, none to the x-axis and jumpHeight to the y-axis.
            rigidBody.AddForce(new Vector2(0, jumpHeight));
        }
    }

    //Function which draws gizmos, showing the computer's calculations in the form of a sphere.
    void OnDrawGizmos()
    {
        //Color of gizmos is blue.
        Gizmos.color = Color.blue;
        //Gizmos is to draw a wire sphere using the grounder.transform's position, and the radius value.
        Gizmos.DrawWireSphere(grounder.transform.position, radius);
    }
}
Edited by Ovicior

Share this post


Link to post
Share on other sites
Advertisement

One option is to do a (short) raycast downwards from the player. If you hit something, you're grounded.

Can you please paste some code that could do that? I'm not sure how that works (On the technical side. I understand the concept). It'd be amazing if you could integrate it into the code I pasted here, but I understand if you choose not to.

Share this post


Link to post
Share on other sites

Unity provides a very simple way to do this - override OnCollisionStay2D(), check if the collider is a ground object, and use that to update some boolean isGrounded flag in your class.

 

In order to do it that way you'd need to have ground colliders only on the tops of ground objects. Otherwise you'd be able to jump off the sides or bottom of things that are marked as ground.

Share this post


Link to post
Share on other sites


In order to do it that way you'd need to have ground colliders only on the tops of ground objects. Otherwise you'd be able to jump off the sides or bottom of things that are marked as ground.

Aye, that's fair.

Share this post


Link to post
Share on other sites

Unity provides a very simple way to do this - override OnCollisionStay2D(), check if the collider is a ground object, and use that to update some boolean isGrounded flag in your class.

 
In order to do it that way you'd need to have ground colliders only on the tops of ground objects. Otherwise you'd be able to jump off the sides or bottom of things that are marked as ground.


Sounds like a feature, not a bug! I love me some wall-jump. Edited by Nypyren

Share this post


Link to post
Share on other sites

 


In order to do it that way you'd need to have ground colliders only on the tops of ground objects. Otherwise you'd be able to jump off the sides or bottom of things that are marked as ground.

Aye, that's fair.

 

Yeah, but I don't want wall jumping. I'm building a little RPG where you can go left or right, and inside buildings if you want. 

Share this post


Link to post
Share on other sites

 

Unity provides a very simple way to do this - override OnCollisionStay2D(), check if the collider is a ground object, and use that to update some boolean isGrounded flag in your class.

 

In order to do it that way you'd need to have ground colliders only on the tops of ground objects. Otherwise you'd be able to jump off the sides or bottom of things that are marked as ground.

 

 

 

Not necessarily, you could stick some logic inside the OnCollisionStay to check that the ground is under the player.

Share this post


Link to post
Share on other sites

Unity provides a very simple way to do this - override OnCollisionStay2D(), check if the collider is a ground object, and use that to update some boolean isGrounded flag in your class.

 
In order to do it that way you'd need to have ground colliders only on the tops of ground objects. Otherwise you'd be able to jump off the sides or bottom of things that are marked as ground.


You could just check the contacts in the Collision2D object. If one has a normal that is pointing up, then you are touching ground.
 
// a max slope of 30 degrees
private static float GroundAngleTolerance = Mathf.Cos(30.0f * Mathf.Deg2Rad);

void OnCollisionEnter2D(Collision2D coll) 
{
  foreach(ContactPoint2D contact in coll.contacts)
  {
    if (Vector3.Dot(contact.normal, Vector3.up) > GroundAngleTolerance)
    {
      // this collider is touching "ground"
    }
  }
}
EDIT: I quoted ground because this would consider any contact that touches the bottom of the player as ground Edited by HappyCoder

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement