- Home /
Object always falling off moving platform
I'm currently working on a game, where I need a ball to be balanced on a platform. And you can move the platform, the problem is when i move the platform the ball won't stay on the platform, it will just fall off. In real life the ball would stay on with a slow speed, but when you move the thing too fast it will fall off. I know you can use the parenting trick, and other coding things. But I need it to be real physics, to make it more realistic. for instance: by using friction or something. I tried changing the friction of the physic material but nothing seemed to happen.
So my question is, can I make it stay on the platform by using pure physics and how? If I can't what alternative methods can I use?
Answer by JimNero_009 · Dec 26, 2015 at 03:37 PM
Speaking from a pure physics point of view, an object will stay motionless on a surface so long as the force trying to move the object is such that it is not enough to overcome the object's coefficient of static friction. In other words, the more friction something has, the more force you need to exert in order to move it. So changing the friction settings of the platform and ball is certainly the right thing to do.
However, with moving platforms in games a common thing to do is directly change the position vector of the platform in order to get the movement. This means that actually no force is being applied to the ball that is resting on top of it - the platform is just being systematically put in different places at a time scale small enough to make it seem as if it's moving naturally. If you look at the velocity of the platform at any given point, though, you will find it to be zero. If you want to achieve a real world physics effect, you would have to apply a rigidbody component to the platform and manipulate its movement by applying forces on it or manipulating the velocity vector in a sensible way. I imagine this is what is being missed right now.
Answer by $$anonymous$$ · Mar 07, 2020 at 06:34 PM
This is an example of how to do what @JimNero_009 is talking about. For more information about how to add force: https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//MonoBehavior has a bunch of methods that are called during
// the lifecycyle of the GameObject that this script
// is attached to. In our case, the platform.
public class CubeControllerLeftRight : MonoBehaviour
{
//We cache these values to avoid uneccessary method calls
// and unecessary object creation
public Rigidbody rigidbody;
public Vector3 force;
// Start is called before the first frame update
void Start()
{
//GetComponent gets a component.
//<RigidBody> filters the components and returns only
// the specified type.
rigidbody = GetComponent<Rigidbody>();
//This vector says no force in the X or Y axis.
//Apply .1 KG of force to the Z access
force = new Vector3(0f, 0f, .1f);
}
void FixedUpdate()
{
//The force Vector3 describes the amount of power in 3 directions
//Input.GetAxis returns -1, 0, or 1 which controls the direction.
//ForceMode.Impulse means that it is a moment of push that adjusts the velocity.
// As opposed to setting the velocity or acceleration in absolute terms.
//The game is played on the Z axis. So, we have left push, no push, right push
// from the perspective of the camera.
rigidbody.AddForce(force * Input.GetAxis("Horizontal"), ForceMode.Impulse);
}
}