I'm trying to make cubes to stack up on a platform which could be moved by inputs. The stacking works but as soon as I'm moving the platform, the cubes tend to roll out of the platform as they were light cart boxes.
I tried a lot of things and read many forums, tutorials and others... but what I can figure out is how I could make my cubes heavier for them to stay in place. I tried:
- A physic material with max/high values for the friction parameters. Looks like "sticking" worked but I don't understand how come they roll ?
- I changed the rigidbody for an higher mass thinking it would make them heavier and in turn add more friction. But it didn't change a thing
- I played with angular drag but it didn't do nothing either
- I tried to add a weight force (mass * gravity) seemed to help but objects keep rolling like "light" feather objects
- I tried to scale up (in the fbximporter) but that didn't do nothing either
So a quick scene setup would be:
- A platform with a script, a mesh collider or a cube collider and a rigid body flaged as IsKinematic. Mass set to 10.
- A cube with a rigid body with mass set to 0.1
- each assigned with a super sticky material
My moving script is quite simple...
public class PlatformControls : MonoBehaviour {
public float speed = 5.0f;
void FixedUpdate()
{
float input = Input.GetAxis("Horizontal");
rigidbody.MovePosition(rigidbody.position + (Vector3.right * input * speed * Time.fixedDeltaTime));
}
}
So could anyone help ? pretty sure that would answer all those "moving platform" problem out there...
UPDATE: Thx for all the answers !
The problem was indeed the "infinite acceleration" since I was moving the platform without gradually increasing the speed. Enough said, here some code
public class Controls : MonoBehaviour
{
public float MaxSpeed = 1.0f;
public float PositionAccelerationTime = 1.0f;
private float CurrentPositionSpeed = 0.0f;
private float InputVelocity = 0.0f;
void FixedUpdate()
{
float input = Input.GetAxisRaw("Horizontal");
// Speed
CurrentPositionSpeed = Mathf.SmoothDamp(CurrentPositionSpeed, input, ref InputVelocity, PositionAccelerationTime, MaxSpeed);
Vector3 velocity = (CurrentPositionSpeed * MaxSpeed * Time.deltaTime) * Vector3.right;
rigidbody.MovePosition(rigidbody.position + velocity);
}
}