I’m incredibly new and ignorant to unity, and to give myself a challenge I figured I’d start with a 2D platformer, figured it’d be straightforward. I’ve hit my first hitch with ground checks, I’m using OnTriggerEnter and OnTriggerExit with two box colliders on my main character, one as the trigger and the other as the physics collider. The problem I find is the trigger collider is not being recorded as colliding with anything despite it extending further than the physics collider. Without the ground checks, I can’t keep the player from being able to fly by spamming space bar. Once again, I’ve been using unity for maybe 3 months, and have no prior coding knowledge, specifics and terms may go over my head, but I’m determined to keep trying.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
Animator Anim;
public float speed;
public Rigidbody2D rb;
public float jumpForce = 10;
public float gravityScale = 10;
private bool isGrounded = false;
private void Start()
{
Anim = GetComponent<Animator>();
}
private void FixedUpdate()
{
Vector3 playerInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0, 0);
transform.position = transform.position + playerInput.normalized * speed * Time.deltaTime;
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Ground")
{
isGrounded = true;
Debug.Log("TriggerEnter");
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Ground")
{
isGrounded= false;
Debug.Log("TriggerExit");
}
}
private void LateUpdate()
{
if (isGrounded = true && Input.GetKeyDown(KeyCode.Space))
{
rb.velocity = new Vector3(rb.velocity.x, jumpForce, rb.velocity.y);
}
}
}