How to do a ground check?
So I tried making my own, but I don't know the issue
using UnityEngine;
public class playerMovementVertical : MonoBehaviour {
public GameObject check;
[HideInInspector]public Rigidbody2D rb2d;
public float jumpHeight = 7.5f;
private bool canJump;
[HideInInspector]public groundCheck script;
private void Awake() {
rb2d = check.GetComponent<Rigidbody2D>();
script = check.GetComponent<groundCheck>();
}
private void Update() {
if (Input.GetKeyDown(KeyCode.Space) && canJump && script.isGrounded) {
rb2d.velocity = new Vector2(rb2d.velocity.x, jumpHeight);
}
}
private void OnCollisionEnter2D(Collision2D colEnt) {
if (colEnt.collider.tag != "Wall" || colEnt.collider.tag != "NoJump") {
canJump = true;
}
}
private void OnCollisionExit2D(Collision2D colExt) {
canJump = false;
}
}
using UnityEngine;
public class groundCheck : MonoBehaviour {
public GameObject objFollow;
public float yOffset;
public Rigidbody2D objRb;
[HideInInspector]public bool isGrounded;
private void Start() {
objRb = objFollow.GetComponent<Rigidbody2D>();
}
private void Update() {
transform.position = new Vector3(objFollow.transform.position.x, objFollow.transform.position.y + yOffset, objFollow.transform.position.z);
}
private void OnCollisionEnter2D(Collision2D col2d) {
if (col2d.collider.name == objFollow.name) {
Physics2D.IgnoreCollision(col2d.collider, col2d.collider);
} else {
isGrounded = true;
}
}
private void OnCollisionExit2D(Collision2D col2d) {
isGrounded = false;
}
}
So I tried making it so the groundChecker ignores the player, that works fine, but I can't jump at all.
Answer by Punksicles · Nov 19, 2021 at 04:50 AM
Looks to me like the Physics2d.IgnoreCollision code is set to ignore all collisions. In the parenthesis you need to specify, separating with a comma, which two colliders are going to be ignoring collision. Instead I would word the if statement with a "doesn't equal" the objFollow.name and get rid of the IgnoreCollision altogether.
I don't work in 2D though, so I'm not sure exactly how much of what I said is the same, but I hope it helps.
Your answer
![](https://koobas.hobune.stream/wayback/20220613045539im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to fix minor jitter/stutter when moving a 2D character using physics in FixedUpdate() 0 Answers
Having my GameObject passing trough floor during one frame ( 2D Platformer ) 1 Answer
Why isn't my character moving? 2D C# Physics 2 Answers
How To Make my Player Glide in 2d platformer 3 Answers
When applying a force to an object on Touch, how do I add a *cooldown* to it? 2 Answers