Falling Platforms (412428)

Hi, I am trying to write a script that will turn on the gravity of a floating platform so that when the user’s fps walker jumps on top of it the platform will start to drop, and then if the player jumps up or off of it the platform will raise back up to it’s original starting position.

The platform has a Rigidbody already applied to it but gravity and kinematics are turned off.

using UnityEngine;
using System.Collections;

public class moveBlock : MonoBehaviour {
	
	void OnControllerColliderHit (ControllerColliderHit other)
    {
		if (other.collider.attachedRigidbody)
		{
			other.collider.attachedRigidbody.useGravity = false;
		}
		
		// Turn this collider into a trigger on startup
	other.isTrigger = true;
	}
	
}

Instaed of turning on or off the gravity, I have seen something like this where the rigidbody was enabled OnTriggerEnter :idea:

I tried using an OnTrigger Enter and enabling the Trigger for that object, but when the user touches it they fall straight through it as it has no mass.

you can try giving a Constant Force to the floor and then control it’s Y force

How do you do that? I’ve not had much practice using C# in Unity and all the examples i’ve seen use JavaScript which I don’t want to use.

Can anyone help me work out how to make a floating platform lower when the fps walker lands on top of it?

//C#
using UnityEngine;
using System.Collections;

public class BoobyTrap : MonoBehaviour {
	
	void OnTriggerEnter (Collider other)
    {
		if (other.attachedRigidbody)
		{
			other.attachedRigidbody.useGravity = true;
		}
		
		// Turn this collider into a trigger on startup
	other.isTrigger = true;
	}
}

If you put say a box collider or mesh collider to the platform you should be able to stand on it till the rigid body it enabled. For some reason platforms seem to get peaple puzzled. Make a trigger box collider on top of the platform and call for it to make you a child OnTriggerEnter That also pushes you up as well

Parent with a line like this

function OnTriggerEnter(other:Collider)
other.gameObject.transform.parent=gameObject.transform;

Then OnEnterStay
for (var child : Transform in transform)
child.position += Vector3.up * 0.01;

These example lines should get you started :smile:

I managed to get it to lower the platform after the controller lands on the platform, though I haven’t got it to raise up again just yet.

I used this code and placed it on the main camera controller, and it will only affect any box with a rigidbody attached to it and hasn’t got isKinematic selected.

using UnityEngine;
using System.Collections;

public class BoobyTrap : MonoBehaviour {
	
	float forcePower = 0.5f;
	Vector3 moveDir;
	
    void OnControllerColliderHit (ControllerColliderHit hit)
    {
		Rigidbody body = hit.collider.attachedRigidbody;
		
		// no rigidbody
		if (body == null || body.isKinematic)
			return;
           
		// Calculate push direction from move direction, 
		// we only push objects to the sides never up and down
		moveDir = new Vector3 (0, hit.moveDirection.y - forcePower, 0);

		// If you know how fast your character is trying to move,
		// then you can also multiply the push velocity by that.
    
		// Apply the push
		body.velocity = moveDir * forcePower;
	}
}