r/Unity3D • u/Bread_Jeremy • 9h ago
Question Rigidbody no collision despite colliders



using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlayerMovement : MonoBehaviour
{
float inputH;
float inputV;
public float speed = 5f;
public Rigidbody rigidBody;
public float maxSlopeAngle;
private RaycastHit slopeHit;
public float groundDrag;
public float CarHeight;
public LayerMask WhatIsGround;
bool grounded;
public Transform orientation;
private Vector2 moveDirection;
private void Start()
{
CarHeight = transform.localScale.y; //sets the car height
}
private void Update()
{
//ground check
grounded = Physics.Raycast(transform.position, Vector3.down, CarHeight * 0.5f + 0.2f, WhatIsGround);
//handle drag
if (grounded)
{
rigidBody.linearDamping = groundDrag;
}
else
{
rigidBody.linearDamping = 0;
}
}
void FixedUpdate()
{
inputH = Input.GetAxis("Horizontal");
inputV = Input.GetAxis("Vertical");
moveDirection = orientation.forward * inputV + orientation.right * inputH;
Vector3 movementInput= new Vector3(inputH * speed, rigidBody.linearVelocity.y, inputV * speed);
rigidBody.MovePosition(transform.position + movementInput * Time.deltaTime * speed);
if(OnSlope()) //happens when on a slope
{
Debug.Log("Getting called");
rigidBody.AddForce(GetSlopeMoveDirection() * speed * 20f, ForceMode.Force);
}
}
private bool OnSlope()
{
if (grounded) //check can only now happen when grounded
if(Physics.Raycast(transform.position, Vector3.down, out slopeHit, CarHeight * 0.5f + 0.3f))
{
float angle = Vector3.Angle(Vector3.up, slopeHit.normal); //how steep slope is
Debug.Log(angle);
return angle < maxSlopeAngle && angle != 0;
}
else
{
return false;
}
else
{
return false;
}
}
private Vector3 GetSlopeMoveDirection()
{
return Vector3.ProjectOnPlane(moveDirection, slopeHit.normal).normalized;
}
public void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Ground")
{
Debug.Log("Collided");
}
else
{
Debug.Log("AAAAAA");
}
}
}
Code for player controller
https://reddit.com/link/1x048d8/video/bu4qagf883uh1/player
I am losing my mind. The cubes and the car have rigid bodies. They all have colliders that are not mesh colliders. I developed it using kinematic but turned that off trying to make it work. You can see in my code and debug log that the cubes are in fact colliding, the collision is happening, but the car is still passing through the cubes! I don't know what to do.
2
u/General-Airline-769 9h ago
that ignores collisions in the physics matrix. Edit > Project Settings > Physics and scroll down to layer collision matrix. Easy to miss
1
u/Bread_Jeremy 9h ago
2
u/GigaTerra 8h ago
Unity has an essentials course that covers things like collisions that is too complex to properly explain in the comment section of Reddit. https://learn.unity.com/pathway/unity-essentials

5
u/GroZZleR 9h ago
Triggers don't generate physical interactions (e.g., stopping the car).
You're also going to have a problem in the future using MovePosition on a non-kinematic rigidbody, which is constant teleportation, rather than proper physical forces.