- Home /
Floor and Sphere collision, strange physic behavior
Hi everyone !
I am having a strange behavior with what I believed was a simple game... I searched on the web for questions and answers but couldn't find anything useful so far.
Let me explain my problem :
I have a Sphere (the simple one, made in Unity) that is my character. It has a rigidbody as well as a sphere collider. The rigidbody uses gravity and is also modified with a script of mine to make the ball go left or right like this :
void Update()
{
movement = Input.GetAxis("Horizontal");
}
void FixedUpdate()
{
Vector3 velocity = rigidbody.velocity;
velocity.x = movement * speed;
velocity.z = Mathf.Clamp(velocity.z, 0, maximumSpeed);
rigidbody.velocity = velocity;
}
Basically, the script handles the X movement as well as a Z limited speed. The Z is handled with a Constant Force component pushing the sphere on Z. (and only on Z)
With that, I have a floor that is a simple cube, scaled, just below my sphere so it can roll over it. The floor is managed with another script that handles the generation of an infinite floor (more of a corridor really).
public void Update()
{
float currentPosition = player.transform.position.z;
if (currentPosition + playerOffsetBeforeNextSpawn >= currentOffset + offsetBeforeNextSegment)
{
currentOffset += offsetBeforeNextSegment;
GameObject segment = PickSegment();
segment.transform.position = new Vector3(0, 0, currentOffset);
}
RecycleOldSegments();
}
The segments are nicely side by side (no space between them). The script uses a pool of segments (the one behind the camera are recycled). So the segments are just moving in Z to continue the corridor.
What goes wrong is this : From time to time, my sphere (capped at 100 z velocity) will jump off, with a random amplitude. There is no script that affects its Y velocity and I know the cause, I just can't get it working correctly !
I think the problem is due to the segments being segments and not a unique mesh. The sphere would collide with the "new" ground and go crazy over it !
I can't see a solution for making it not jumping so randomly... :(
have the following constraints :*
My sphere need to be able to jump so I can't used the rigidbody constraints.
I need to create the corridor segment by segment and can't simply use a unique mesh.
With all that said, I am posting here to know if anyone of you already faced this problem? Is there a better way to achieve what I am trying to do? How? Maybe the constant force is not appropriate here, how can I achieve an automatic acceleration without it and no input ?
Thank you for your time and many thanks in advance ! =D
PS : This behavior happens even without doing anything (movement = 0) and the sphere rolling straight ahead!
I have the same problem. To be honest I didn't read everything you wrote but my ball moves horizontally along planes and one in a while it jumps just like you said. Seems like it's collides with the edges or gaps of the planes. There's another thread like this one here but without an answer. I'm still waiting :/
Answer by killradio · May 11, 2014 at 08:48 AM
Hi again,
I'm keeping track of everything I tested so far to resolve this problem, without any luck though...
Here are the few things I tested with no changes :
Set the offset of my "segment factory" so the segment of the ground are not side by side but overlap a little on the Z axis.
Removed the constant force and applied a simple, fixed, Z velocity to my sphere.
Wrapped the ground in a "Ground" Layer, and set it not to collide with itself (and repeat first test).
Changed my factory to work and create segments in the "FixedUpdate".
Those modifications did not change the jump effect.
I also modified the offset of my segment factory to make the segment side by side with a small gap in between. That way we can clearly see the sphere jumping when changing segment (normal behavior).
In the inspector, all my created segment have the same X and Y. Only Z changes. So their box collider are nicely side by side (even linked when the offset is lower than the size of the segment).
To be sure that I am targeting right where the problem is, I made my sphere fly over the ground (0.5 Y above) and froze the Y position in its rigidbody. The bug disappear.
Unfortunately, I can't keep it that way as my sphere has to be able to go higher or lower. But at least, I'm sure that this is the problem : my sphere collides with the new segment (from time to time), even if its on the same height as the previous one.
Anyone having this issue? I'm still new to Unity so maybe I'm skipping something important here... :(