[RESOLVED] Two rigidbodies not colliding (even with traditional fixes, e.g. raycasting, collision detection)
I'm new to Unity and trying to figure out what would seem to be a basic physics problem but it has me stumped, even after spending days researching various forums and boards.
I'm trying to prototype a simple foosball-type game where a rod with a paddle attached to it rotates (by player input) and knocks the ball away. But instead, the ball and paddle hardly interact at all.
The Inspector Details:
The rod is the parent, has a rigidbody and is set to kinematic. It contains a capsule collider. Collision detection is set to continuous speculative. (Yes, I'm aware it's more costly for the physics... I'm just trying desperately to solve my problem).
The paddle is the child, has a rigidbody and is also set to kinematic. It contains a box collider. Collision detection is set to continuous speculative.
The ball has a rigidbody and is affected by gravity (and is obviously not kinematic). It contains a sphere collider. Collision detection is set to continuous. It also contains physics material for bounce.
The Scripting Details:
The rod contains the main controller script (moving left or right and rotating forwards or backwards) which is based on MovePosition/MoveRotation.
The paddle contains a script that's supposed to add force to the ball on collision (though to be honest, I'm not sure what it's doing).
All three gameobjects (i.e. rod, paddle, ball) contain Daniel Brauer's DontGoThroughThings script that I found during my research but it doesn't seem to be working in my case.
Here's The Code:
PlayerController
What Am I Doing Wrong?
I'm at a dead end in terms of troubleshooting.
I thought maybe it's a collision detection issue because the paddle is moving too fast. But can that still be the case with both the DontGoThroughThings script and Continuous (Speculative) collision detection applied to everything?
Alternatively, I thought I might be using the wrong code to move the player (I'm so new to Unity, I was using the transform.translate function to move the rod at first, LOL). But after researching, it seems like MovePosition or AddForce are the two best options for rigidbody objects.
Thanks in advance for any and all help!
,
Answer by WestBayGC · Oct 31, 2020 at 06:26 AM
I finally solved the issue, for anyone who stumbles on this question later:
//There were TWO problems; one code-based, the other inspector-based.
//First, I scripted both input and movement in Update. The correct method is to write the input code in Update but write the movement code in FixedUpdate... because, physics. (Yes, I know the code below is awful... I'm still learning to program...). Here's a YouTube video explaining the concept:
https://www.youtube.com/watch?v=MfIsp28TYAQ
The new code solved the issue of the rod itself not acting on the ball with physics. However, that did not solve the problem visualized in my original GIF of the paddle not interacting with the ball.
Which leads me to...
//Second, I had two rigidbodies on a compound collider (i.e. both the parent-rod and the child-paddle had rigidbodies). I had to remove the rigidbody on the child-paddle and now it interacts as expected with the ball in a physics-based manner.
Here's the Unity documentation discussing this issue:
Answer by f1uke · Oct 30, 2020 at 10:26 AM
I have the exact same issue. The DontGoThroughThings helped me with collision detection, but there is also a speed issue I think. With lover speeds, the to two objects interact more with each other. With higher speeds the two objects hardly interacts. I am using a rigidbodies and physics to fire a bullet, but it's not working at high speeds. Raycasting is not a solution for me, because I need the bullet traveltime. I have also been searching for a solutions for days, but still no luck.
Hi @f1uke. Sorry, I'm completely new to Unity/game dev/program$$anonymous$$g myself and have no idea how to help. I'm guessing your problem is more of (what I call) an "inspector"-based issue than a code issue. But I could be wrong. It turns out I didn't need the DontGoThroughThings script on my paddle because it wasn't even supposed to have a rigidbody (see my answer to my own question). However, I still have that script on the rod. If you made it as far as finding the DontGoThroughThings script on your own, it seems like you've already seen the stuff that I would have thought could be useful for you. But please let me know if/when you find the solution, because it's likely I'll need it at some point in the future, too. Good luck!
Your answer
Follow this Question
Related Questions
Spawned objects sink through ground, when over 3 clones 0 Answers
Collisions without colliders 0 Answers
GameObject not colliding properly with a collider! 3 Answers
How i prevent 2 obejcts with kinematic checked in both rigidbodies collide? 3 Answers
[SOLVED] Colliders aren't working properly. Clipping through ground. 1 Answer