r/Unity3D • • Oct 07 '26

Question Rigidbody no collision despite colliders

Player car
player car
cubes
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

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 Upvotes

10 comments sorted by

5

u/GroZZleR Oct 07 '26

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.

0

u/Bread_Jeremy Oct 07 '26

I know the triggers don't but I dont know what to do after this. Can I make it work with kinematic on?

2

u/GroZZleR Oct 07 '26

Unchecking "Is Trigger" and changing your MovePosition line ...

rigidBody.velocity = movementInput * Time.deltaTime * speed;

... should at least get you something.

It's going to be a long and difficult road to create a physically accurate driving controller.

1

u/Bread_Jeremy Oct 07 '26

nothing happened when i put it in the triggerEnter check

4

u/Chameleonch1ck Oct 07 '26 edited Oct 07 '26

are you still using isTrigger colliders? I mean that trigger colliders using methods like: OnTriggerEnter, OnTriggerStay, OnTriggerExit. For non trigger use other methods: OnCollisionEnter, OnCollisionStay, OnCollisionExit.

So you need to replace OnTriggerEnter method with OnCollisionEnter method and uncheck "Is Trigger" in colliders

2

u/[deleted] Oct 07 '26

[removed] — view removed comment

0

u/Bread_Jeremy Oct 07 '26

Is this how it should be?

4

u/GigaTerra Oct 07 '26

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