OnCollisionEnter() not working

image

My console won’t display “bumped into a Wall” when I move my player into one. I’m using the visual studio community because I couldn’t get IntelliSense to work on VS code. I have a collider on my walls and a collider and rigid body on my player.

Do you have a rigidbody?

Without at least one rigidbody, there can be no collision events.

Yeah, I have a rigid body on my player. Whenever I move my player into a wall, it bounces back and forth a bit before phasing through. I searched up a couple of solutions like changing collision detection from “discrete” to “continuous” and changing the sleeper threshold in the physics settings but the problem’s still there.

Edit: I tried adding a rigidbody to my walls but when i freeze rotation and position on them my player object just goes through them

image
These are the settings on my player

Sorry for leaving so many replies but I just decreased my player speed and it stopped going through but the player object still jitters back and forth when it touches the wall and unity still isn’t detecting the collision

Is your player’s rigidbody kinematic? Your walls are treated as static if they don’t have a rigidbody (or maybe you set them too to kinematic when adding the rigidbody). The collision event will only trigger when at least one of the rigidbodies is dynamic (the isKinematic checkbox is unchecked).

I didn’t look at your picture properly. Are you moving the player with the transform? That sometimes make the physics not work properly. Rick precisely addresses this in one of the lectures from Obstacle Course (I assume you are doing that course right now). The jittering is normal when moving something through the transform instead of through the rigidbody, but the collision should still be registered. Where do you have the script attached? It should be on the obstacles if I remember correctly from the course.

I just attached the script to my wall pre-fab and unity is detecting the collision now ( I previously had it attached to an “empty” object or whatever it’s called that I used to group all my walls). I really appreciate the help because I’ve been stuck here for a day and a half and it was really messing up my progress. Like seriously, thank you.

Edit: oh and on a side note, how do I move the player “through the rigid body” instead of through the transform

1 Like

Hello, just to preface, I don’t want this to seem rude because I know it sometimes does.

Unity has a really great manual with examples. I’ll link you to the rigid body (Here) section and you can have a read.
Here is a tip I read from the physics section while looking for you.

“If you are directly manipulating the Transform component of your object but still want physics, attach a Rigidbody and make it Kinematic.”

How I go about searcing for answers is use the google search “unity doc…” then type my question in. In example this is what I got from searching “unity doc rigidbody move”. Its a link to the unity doc with an example and explanation link

Hope that helps so you dont get stuck for days again. Just knowing what to google is just as important as knowing nowadays.

You’re welcome! Struggling is an integral part of learning. Important is that you managed to solve it and now will know where to look when a similar situation arises.

There is an important unity concept that you need to learn here. Physics callbacks like OnCollisionEnter(), OnTriggerEnter(), etc. don’t travel upstream or downstream the hierarchy unless there is a Rigidbody component in the parent. If you don’t have a Rigidbody, you need to place the script with those callbacks in the exact same object that has the collider/trigger. Else it doesn’t work.

The exception is if you have a parent with many children (each child with a collider), you can place the script in the parent so long you also put a Rigidbody there, as the rigidbody “scans” the children and acknowledges all colliders downstream. However, it doesn’t work upstream. If you had a rigidbody in a child and a collider in the parent and all the other children, that rigidbody will only detect the collider in that same child and any other children that sole child could have. Be aware of this anytime an OnCollision/OnTrigger doesn’t seem to work.

To move an object through the rigidbody I would direct you to the rigidbody documentation like the other poster mentioned. However, if you have a dynamic rigidbody you should look to the AddForce() and AddTorque() methods. For a kinematic rigidbody you should look at MovePosition() and MoveRotation(). If you use any of those 4 methods, is important to use them inside FixedUpdate() instead of the normal Update(), as FixedUpdate() is specially used by the unity engine to handle the physics.

My final recommendation is that whenever you have a doubt about a component, click the little question mark to the right of the component name in the inspector and it will send you directly to the documentation page of that specific component.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms