Controllable Ball Physics

I’m making a game in which your character is just a rolling ball (RigidBody + SphereCollider). You can move left, right, forward, and backward via the arrow keys, which move the ball using the AddForce function on the RigidBody. (There’s also a jump, but this is not relevant to the question.) This works for the most part, except that it makes the ball appear to slide, rather than roll when it moves. So I tried also putting in an AddTorque function to make the ball rotate properly. This works as well, except that now it’s possible to push against and roll up a vertical wall! Using AddTorque alone doesn’t have this problem, but it also moves as slow as dirt (and does not allow any controls if the ball is not touching the ground). I like the feel of the controls using both AddForce and AddTorque, but I can’t allow wall-climbing. Any idea how to make that happen? I’ve tried tweaking all the physics material parameters, the mass, and friction and such, but nothing seems to completely prevent it. (I don’t want to just cast a ray and prevent movement if a wall is adjacent - you should be able to move along walls and up slopes, just not if the slope is steeper than, say, 45 degrees, although even steeper slopes should be possible if you get enough speed first - think Sonic the Hedgehog.)

Edit: Oh, also raycasting to make sure there’s no vertical surface in the way won’t work - because sometimes there are movable objects you’ll need to push, and you shouldn’t be able to climb up them either, if they’re against a wall. I’ve also tried adding a slight downward push to AddForce, but this either isn’t enough to keep it from wall-climbing, or it’s so strong it makes the ball bounce upward off the ground, doesn’t seem to be a comfortable middle ground.

Try doing a spherecast downward from the current balls position. like 0.1 units. If there is no hit, or the Vector3.Dot(hit.normal, Vector3.up) < 0.5 then simply don’t add the torque. This gives you ball control in the air, without rolling up vertical surfaces.

Not a bad solution. I was already casting a down-ray every frame (to determine if you can jump), so it works to use it for multiple purposes. It required a bit of tweaking, since I was being generous with the minimum ground clearance required for jumping. (You can still climb a little bit, but not enough to get on top of anything.)

Anyhow, it’s better than what I had before, which was making all static objects use a frictionless material. That works, but then there’s no friction on the tops of objects either, so it’s like skidding around on ice. I was seriously considering making all objects in multiple pieces so that there’s friction on top but not the sides, but that would’ve been pretty ugly.

hi,

i’d be interested in knowing how you did your script cause i’m facing issues for ball controller…

thanks