What would be the best way to stop a box sliding off a platform when the platform is moved?
I have a moving platform which is a kinematic rigid body with a box collider. I've tried a couple of ways of controlling it (it moves in and out, rotates on its axis and moves up and down) - see method 1 and 2 at the bottom of my post.
Both methods result in the box sliding off the platform when the platform moves.
The platform is a kinematic rigid body with a box collider. The box is a non-kinematic body with a mesh collider. The platform and box both use a physics material.
Any pointers or suggestions would be helpful. By the way, I have played around with the physics material, and the mass/ drag settings of both the platform and the box, with little success.
Kinematic rigid body controller Method 1:
function Update () {
transform.Translate(Input.GetAxis("Horizontal") *Time.deltaTime*10, 0, Input.GetAxis("Vertical") *Time.deltaTime*10);
if (Input.GetButton ("platform_up"))
transform.Translate(0,Time.deltaTime*3,0, Space.World);
if (Input.GetButton ("platform_down"))
transform.Translate(0,-Time.deltaTime*3,0, Space.World);
}
Kinematic rigid body controller Method 2:
var speed : float = 10.0;
var rotationSpeed : float = 100.0;
function Update () {
// Get the horizontal and vertical axis.
// By default they are mapped to the arrow keys.
// The value is in the range -1 to 1
var translation : float = Input.GetAxis ("Vertical") * speed;
var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
// Make it move 10 meters per second instead of 10 meters per frame...
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
// Move translation along the object's z-axis
transform.Translate (0, 0, translation);
// Rotate around our y-axis
transform.Rotate (0, rotation, 0);
if (Input.GetButton ("platform_up"))
transform.Translate(0,Time.deltaTime*3,0, Space.World);
if (Input.GetButton ("platform_down"))
transform.Translate(0,-Time.deltaTime*3,0, Space.World);
}
//can I limit the amount the platform can go up and down by in world space - say a lower limit of 0,0.5,0 and upper limit 0,9,0
//}
Update of this main question as pointed out by Joshua. Many thanks for your help. *RESOLVED*
Description of my scene:
I have a plane with a mesh collider serving as a "ground".
I have a box, tagged as box.
I have a platform, tagged as platform with "Kinematic rigid body controller Method 2": script from above controlling its movement.
I have a platform box collider set to trigger as a child of the platform. This has the following script attached:
public var Platform: GameObject;
function OnTriggerEnter (col : Collider) {
var box = GameObject.FindWithTag ("box");
box.transform.parent = Platform.transform;
}
function OnTriggerExit (col : Collider) {
var box = GameObject.FindWithTag ("box");
box.gameObject.transform.parent = null;
}
Result:
When the platform is moved under the box and collides with it, the box becomes a child of the platform, and follows it around exactly. This is what I wanted!
When I lower the platform to the ground the box then ceases to become a child of the platform and sits on the ground, and the platform can be moved away.
Please note this doesn't work perfectly, as there are errors and pops when the box comes to rest on the ground, but I can live with these as I'm just prototyping at the moment.
All workings out and problems, workarounds follow on the next few questions.
SM.
Thanks Joshua. I've tried adding a trigger to the platform - it doesn't make the box become kinematic. I also tried the OnCollisionEnter whihch I added as a script to the box - it doesn't become a child of the platform - perhaps I need to add something else to the box? The platform has been tagged as "platform" by the way.
– anon26236005I also tried animating the platform to see if it was anything to do with the speed/ accelaration of it affecting the box - but the same thing happens - the box slides off the platform.
– anon26236005Could you tell my why the trigger doesn't make it kinematic? It does if you tell it to through script.. (OnTriggerEnter/Exit). And the second method should definitely work. You're probably doing some simple small step wrong. Please try again and think about each stap, because the solutions I posted work.
– anon73820239