- Home /
Sphere falls through moving diagonal surface
Hi, I really need help with this.
I'm making a 3D sidescrolling game where you have to roll a ball, and the rest is done by physics. You have to jump 'canyons' and overcome other obstacles.
Up till now everything was going alright, but now I made a canyon with a floating platform that goes back and forth from left to right. The player first has to jump onto the platform and then wait till it goes across again, so he can jump to the other side.
I used two (diagonal) ramps which I made in 3ds max and gave a Mesh Collider. The player has a Sphere Collider.
But the ball just 'falls' through the ramps as soon as the platform starts moving:
As long as the platform is idle, nothing's wrong.
If I use only horizontal and vertical Cubes, the ball doesn't go through.
If I cover the ramps with (diagonal) Unity Cubes, the ball WILL still go through.
It seems that the diagonal factor messes everything up. But this is the core of my game, if I can't get this to work, the whole game is worthless.
I read all the other problems concerning GameObjects going through things at high speed or when they're really small, but none of those apply to my problem.
I tried the DontGoThroughThings-script, but that only checks the direction the ball is going in, and that's not the direction in which the ball is hitting the ramps.
Then I wanted to create 8 raycasts (at 0, 45, 90, 135, 180, 225, 270 and 315 degrees) from the position of the ball, but I think then the ball would never move again because the x- and y-movements will always be completely restricted if one ray hits anything.
What I need is to restrict the movement in only the angle at which it hits the ground (or ceiling or wall or any object), but to my knowledge this isn't possible.
The whole project can be downloaded here
Player, Camera, and Floater have a script.
The Floater is inside Track > TrackCanyon2 (see the image)
Game controls:
- Roll ball: spacebar
- Move camera: arrows/WASD
- Reset camera: V
I'm at a loss here! Any help would be greatly appreciated.
Answer by fueldown · Jul 10, 2014 at 07:59 PM
they way you are setting the floating platform's position does not agree with the physics. Don't use transform.position to move the platform. Put a rigidbody on it and use rigidbody.velocity to move the floater. It will respect the ball physics.
I do seem to get some improvement; the ball doesn't sink through immediately. But now the ball sinks through just very slowly and gradually. I changed the tranform.position-line to this: rigidbody.velocity = new Vector3(newX-oldX, 0f, 0f);
(I did turn gravity off on the Floater, and restricted its position on the y- and z-axis, and rotation on all axes. With 'Collision Detection' to Continuous it still sinks through. With Continuous Dynamic it seems to last longer but eventually the whole game freezes up.) Am I still doing something wrong?
I managed to 'fix' it by putting hidden BoxColliders
inside the diagonal ramps, enabling Is Trigger
on those BoxColliders
, and adding a tiny script to them with an OnTriggerStay()
function that pushes the sphere up when it is touching the hidden BoxCollider
.
But none of this would have been possible without your help!
I of course tried things like this before, but when I use transform.position
, the sphere still falls through.
I'm gonna wait just a tiny bit longer to see if anyone has a solution that doesn't require hidden elements that cloud up the Hierarchy, but otherwise I'm gonna mark this answer as correct.
Thank you so much for your help!
I just discovered myself, that if you have smooth sphere collision property on a mesh collider with convex mesh, it ignores the physics for some reason. I may be wrong, but my car is going through rocks that has convex mesh and the smooth sphere collision on. Idk if it's something because of my setup, but worth the check. :)
Answer by Razacx · Jul 11, 2014 at 02:59 PM
When moving the platform with transform.position, the ball collider will clip the mesh collider of the platform causing it to fall trough the collider. There are two possible ways to solve this.
1) Parent the platform to the ball on collision so the transform.position movement will be inherited. Unparent the platform when the ball leaves collision.
2) Attach a rigid body to the platform and move the platform using physics.
Thank you for your answer.
I couldn't get the first option to work, but I did learn something new from that, that's a pretty neat trick. Your second option confirms fueldown's answer as the right way to go, thanks for that.
So I would like to give you thumbs up, but I guess that'll have to wait until I have 15 reputation..
Your answer
Follow this Question
Related Questions
first person controller prefab falls through floor 1 Answer
Player Falling Though Floor After Build With Unity IPhone 0 Answers
OnTriggerEnter is not running! Need help with collision. 2 Answers
Sphere vs Capsule collider collision resolution 0 Answers
How to move an object over a distance in a direction? 2 Answers