0
\$\begingroup\$

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!

EDIT: below are screenshots of configs of Grid, gound, and GroundCheck in that order enter image description here enter image description here enter image description here

\$\endgroup\$
6
  • \$\begingroup\$ You could first move out the Debug.Log("test") from the condition and check if it is a collider problem or a condition problem. At 0:45sec in the video it marks the ground with a tag but it is not possible to see if you did the same with your ground from the screenshot \$\endgroup\$
    – Zibelas
    Sep 30 '20 at 8:29
  • \$\begingroup\$ Is your GroundCheck collider set as "IsTrigger"? Please make sure you show us the configuration of both objects that are supposed to be involved in the collision. \$\endgroup\$
    – DMGregory
    Sep 30 '20 at 9:27
  • \$\begingroup\$ @Zibelas I've added more screenshots, I did tag ground with "Ground". \$\endgroup\$
    – jeb2
    Sep 30 '20 at 9:33
  • \$\begingroup\$ @DMGregory please see screenshots added \$\endgroup\$
    – jeb2
    Sep 30 '20 at 9:34
  • \$\begingroup\$ Alright, there are still a few questions. From the code of Grounded, it looks like it should go to the GroundCheck child gameobject of your player, but you have only the collider there (at least the start method would suggest that). What is your actual ground gameobject under the grid? since you have the tilemap on the grid directly. @DMGregory is IsTrigger needed when he uses OnCollisionEnter2D? \$\endgroup\$
    – Zibelas
    Sep 30 '20 at 9:57
2
\$\begingroup\$

The middle screenshot is a duplication of the first, but assuming it is your grounded script, try to do the following changes.

  1. First you should relocate your tilemap. Currently you have it on the grid (which will work) but if you really want it as a child of the grid. The grid does not need to be tagged with Ground. If it is a child of the grid, you can have a second tilemap with things like background, a third maybe with foreground. You get the point.
  2. The grounded script should be on the player. Just place it on GroundedCheck. Fix the start method. Either you assign the Player in the editor OR load the player in your start method (if you load it from start, make it private)
  3. It should work now but the character will hover over the ground. Your groundCheck should be marked isTrigger and your code should change from OnCollisionEnter2D(Collision2D collision) to OnTriggerEnter2D(Collider2D other). collider can be deleted in the condition to if(collision.tag == "Ground")
\$\endgroup\$
1
  • \$\begingroup\$ Thanks, this worked perfectly. I just needed to use the grounded script on the player and declare the Player variable and it works. \$\endgroup\$
    – jeb2
    Sep 30 '20 at 13:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .