Unity not registering collision between cube and plane when colliding at angles >90 degrees

i have a very simple script that allows a cube to move forward and backwards and also rotate using certain keys. there is also a 200x200 plane that should prevent the cube from going below it. when the cube collides with the plane normally, the plane pushes it back. however, when the cube collides with the plane at any angle greater than 90 degrees, the collision seems to not register. even when colliding with the plane while going backwards at a 90 degree angle, the cube goes through with no pushback from the plane. i tried making the cube collider bigger than the plane itself, but i still have this problem. the plane pushes back, but instantly once the angle becomes greater than 90 it goes right through. here is the code

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditor.Callbacks;
using UnityEngine;
using UnityEngine.XR;

public class collisionthing : MonoBehaviour
{
    Animator anim;
    Rigidbody rb;
    void OnCollisionStay(Collision collision) {
        if (collision.gameObject.CompareTag("ground")) {
            transform.Translate(Vector3.up);
        }
    }
    // Start is called before the first frame update
    void Start()
    {
        Application.targetFrameRate = 60;
        transform.position = Vector3.zero;
        rb = GetComponent<Rigidbody>();
        rb.freezeRotation = true;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.W)){
            transform.Rotate(-1,0,0);
        }
        if (Input.GetKey(KeyCode.A)){
            transform.Rotate(0,-1,0);
        }
        if (Input.GetKey(KeyCode.S)){
            transform.Rotate(1,0,0);
        }
        if (Input.GetKey(KeyCode.D)){
            transform.Rotate(0,1,0);
        }
        if (Input.GetKey(KeyCode.Mouse0)){
            transform.Rotate(0,0,-1);
        }
        if (Input.GetKey(KeyCode.Mouse1)){
            transform.Rotate(0,0,1);
        }
        if (Input.GetKey(KeyCode.Q)){
            transform.position += transform.forward*0.2f;
        }
        if (Input.GetKey(KeyCode.LeftShift)){
            transform.position += -transform.forward*0.2f;
        }
        if (Input.GetKeyDown(KeyCode.Space)){
            transform.rotation = Quaternion.identity;
            rb.velocity = Vector3.zero;
            rb.angularVelocity = Vector3.zero;
        }
    }
}

I’m not sure if this is your issue, but you shouldn’t directly set a rigidbody’s transform positions like that. Either adjust the velocity, apply force, or use Rigidbody.MovePosition() to move a rigidbody while still respecting physics and not doing an error prone teleport.

1 Like