Moving Platforms help

Hi, I'm new to Unity and I'm trying to get a platform with a rigidbody to move in either the X, Y or Z axis automatically with a script that allows me to modify the values for each different platform it's applied on.

I've tried looking at other examples all of which use Javascript, however I have to use C# and every time I try to convert some of the examples I don't get it completely right.

I need the platform to move in a single direction from a starting point and move a certain distance and then back again.

I also have a script that lowers a platform when a controller lands inside on it, but I haven't got anything to make it raise up again.

using UnityEngine;
using System.Collections;

public class BoobyTrap : MonoBehaviour {

    float forcePower = 1.25f;
Vector3 moveDir;
    Rigidbody body;

    void OnControllerColliderHit (ControllerColliderHit hit)
    {
    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;
}
}

A word of caution with your current script: a non-kinematic rigidbody platform will be capable of being pushed around by physic, if I recall correctly, a kinematic rigidbody is often preferred for moving platforms (otherwise they'll become skewed and shifted if something like a baseball or other physics body collided with it).

That being said, I would think that writing a driver for your platforms that resides within FixedUpdate() is much more in line with your purpose, and if they change behavior with player contact, you could just set up a member variable that gets flagged so the driver knows to change.

I think rigidbody.MovePosition(Vector3) might be of interest to you, as then you can calculate on your own path of the platform between two positions. Easiest way (in my opinion) would be to utilize Vector3.Lerp(initialPosition, finalPosition, t) to find the current desired position of the platform, and then modifying the value of t with t = Mathf.Sin(Time.timeSinceLevelLoad). However, there are plenty of other ways to calculate the movement of a platform, that is just one example

I would recommend making animations for your platforms. That way, little scripting is needed, and the object can still collide with it.

Have you ever looked at Configurable Joints. You can set the TargetPosition then use a JointDrive to move the platform towards that point. Then when the platform reaches the bottom, switch the target to the point on the top. Add the configurable joint to your platform, then set the drives to position and set the spring and damper to whatever you want. You can even dynamically change them to reflect the player's velocity on impact. I'd give you a code, but I don't have one at the moment. Sorry.

You could also look at the links below. This question has come up many times before.

http://answers.unity3d.com/questions/10943/moving-platform

http://answers.unity3d.com/questions/4652/how-to-get-a-character-to-move-with-a-moving-platform

static var Waypoint : boolean = true;
var pointB : Transform;
private var pointA : Vector3;
var speed = 1.0;

function Start () {
    if(Waypoint == true) {
             pointA = transform.position;
             while (true) {
            var i = Mathf.PingPong(Time.time * speed, 1);
            transform.position = Vector3.Lerp(pointA, pointB.position, i);
            yield;
            }
        }   
}

so this little script should help u... simply drag it on the platform, then make a empty in the 3dworld and set it as point B in the inspector.... u can change the speed it moves in the inspector as well... and it should work with a rigidbody!