I know this is a common issue, I’ve looked everywhere and can’t find a solution. I have a character with a RigidBody2D as it’s collider, and a block object with a BoxCollider2D. I have tried setting it to trigger, not trigger, either way it just falls right through. My block has the tag “ground” set on it, but it seems like either way it’s not detecting the collision(I debugged by having it print upon collision enter). Here’s my code, sorry if it’s sloppy, took it from another game I’m making lol.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public Magic[] Magics;
public bool isGrounded;
public Rigidbody2D rb;
public float walkSpeed = 7;
public float runSpeed = 10;
public float gravity = -12;
public float jumpHeight = 1;
[Range(0, 1)]
public float airControlPercent;
public float screenHalfWidthInWorldUnits;
public float turnSmoothTime = 0.2f;
public float speedSmoothTime;
float speedsmoothVelocity;
float currentSpeed;
float velocityY;
float velocityX;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
}
private void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "ground")
{
isGrounded = true;
}
}
private void OnTriggerExit2D(Collider2D collision)
{
isGrounded = false;
}
public void Update()
{
if (Input.GetMouseButtonDown(1))
{
foreach (var spell in Magics)
{
}
}
float inputX = Input.GetAxisRaw("Horizontal");
float velocityX = inputX * walkSpeed;
transform.Translate(Vector2.right * velocityX * Time.deltaTime);
if (transform.position.x < -screenHalfWidthInWorldUnits)
{
transform.position = new Vector2(screenHalfWidthInWorldUnits, transform.position.y);
}
if (transform.position.x > screenHalfWidthInWorldUnits)
{
transform.position = new Vector2(-screenHalfWidthInWorldUnits, transform.position.y);
}
Vector2 input = new Vector3(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
Vector2 direction = input.normalized;
bool running = Input.GetKey(KeyCode.LeftShift);
float animationSpeedPercent = ((running) ? currentSpeed / runSpeed : currentSpeed / walkSpeed * .5f);
animator.SetFloat("speedPercent", animationSpeedPercent, speedSmoothTime, Time.deltaTime);
if(isGrounded == true)
{
rb.gravityScale = 0;
}
else
{
rb.gravityScale = 1;
}
if (Input.GetKeyDown(KeyCode.W))
{
Jump();
}
void Jump()
{
if (isGrounded == true)
{
float jumpVelocity = Mathf.Sqrt(-2 * gravity * jumpHeight);
velocityY = jumpVelocity;
}
}
}
float GetModifiedSmoothTime(float smoothTime)
{
if (rb.gravityScale > 0)
{
return smoothTime;
}
if (airControlPercent == 0)
{
return float.MaxValue;
}
return smoothTime / airControlPercent;
}
}
Thanks in advance.