I'm making my first Unity platformer and it occurred to me that I never stopped my player from jumping infinitely. I tried the typical solution of using a boolean value that is switched by jumping and colliding with the ground. The problem is even though my player does collide with the ground OnTriggerEnter2D doesn't seem to detect it.
And yes I have triple checked that the ground has the correct tag.
I tried using a raycaster solution but that didn't seem to work either. I have the code printing out the tag of any object it collides with so I know it's not detecting the ground at all.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
//movement variables
public float movex;
public float speed;
public float jumpforce;
//interactivity variables
public Rigidbody2D rb2d;
private bool isJumping;
Vector3 startingPosition;
// Use this for initialization
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
startingPosition = transform.position;
}
// Update is called once per frame
void Update()
{
// localScale;
if (Input.GetKeyDown(KeyCode.Space) && !isJumping)
{
rb2d.velocity = new Vector2(rb2d.velocity.x, jumpforce);
isJumping = true;
}
}
void OnTriggerEnter2D(Collider2D col)
{
Debug.Log(col.tag);
if (col.gameObject.CompareTag("Floor"))
{
isJumping = false
}
}
}