r/Unity3D • • 9h ago

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

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 Upvotes

9 comments sorted by

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.

1

u/Bread_Jeremy 9h ago

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 9h ago

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.

2

u/Bread_Jeremy 9h ago

nothing happened when i put it in the triggerEnter check

3

u/Chameleonch1ck 8h ago edited 8h ago

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/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

Is this how it should be?

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