Moving Platform

I am looking for a script to move a game object up and down so it can be used as a sort of elevator, it’s just a flattened cube, also the player controls a marble(sphere) if that changes anything.

Hello

If you your looking into making an elevator type I can help, what I do is create a box and remove everything so you can just see the outline put this before the elevator as a trigger add this script to go up:-

var Player: Transform;
var Elevator: Transform;

var to: Vector3;
var from: Vector3;
var elespeed: int=5;
var boolswitch: UpDown;

function OnTriggerStay (other: Collider) {
boolswitch= FindObjectOfType (UpDown);

if (boolswitch.eleswitch ==1) {
if (Elevator.transform.position.y <from.y) {

print (boolswitch.eleswitch);
Player.parent = Elevator;

Elevator. transform.Translate(Vector3.upTime.deltaTimeelespeed);
}
}

}

then create another trigger on the elevator itself add this script to make it go up or down:-

static var eleswitch: int;
var trigger: int=1;
var Player: Transform;

function OnTriggerStay (other: Collider) {
eleswitch =trigger;
Player.parent =null;

}

then finally add another trigger on the top floor and add this script:-

var Player: Transform;
var Elevator: Transform;

var to: Vector3;
var from: Vector3;
var elespeed: int=5;
var boolswitch: UpDown;

function OnTriggerStay (other: Collider) {
boolswitch= FindObjectOfType (UpDown);

if (boolswitch.eleswitch ==2) {
if (Elevator.transform.position.y >to.y) {

print (boolswitch.eleswitch);
Player.parent = Elevator;

Elevator. transform.Translate(-Vector3.upTime.deltaTimeelespeed);
}
}

}

So to some up you have 3 triggers and 3 scripts, two trigger scripts, so basically you walk into a trigger when you get on the lift, it will go up then walk off, to come back down you have the third trigger at the top.

I hope I’ve explain this well, im a noob but I got all of this from this cool tutorial I found

If your after a platform that moves up and down itself, I cant help you there, im looking for the same thing

I’ve got another tidbit that may help you…

Place these functions on your CharacterController, and it will assure your character adheres to the elevator.

void OnTriggerEnter(Collider t)
    {
        if (t.gameObject.tag == "Platform")
        {
            gameObject.transform.parent = t.gameObject.transform;
        }
    }

    void OnTriggerExit(Collider t)
    {
        
        if (t.gameObject.tag == "Platform")
        {
            gameObject.transform.parent = null;
            Debug.Log("Unparented");
        }
    }

I tag the target platform(s) with the “Platform” tag to define them, and in this manner the CharacterController, which would otherwise just fall through them… does not.

I thiiiiiink that works. I copied it from my own blog, so I’d sure hope it does. It should give an idea either way.

Hope that helps in a slight slight way!

-BSpline

you could also just create animations for the platform and trigger them. Unity has script already available for triggers. Just another way to do what you want.

I’m using this script for my moving platform (used as elevator) [C#]

using UnityEngine;
using System.Collections;

public class moverPlat : MonoBehaviour {

	
	public int dirPlat;
	int frameCount;
	

	void Update () 
	{

		frameCount++;	
		
		if (frameCount == 470)
		{
			frameCount = 0;
			
			if (dirPlat == 0)
				dirPlat = 1;
			else
				dirPlat = 0;
		}
		
			
		
		if (dirPlat == 0)
		{
			transform.Translate(Vector3.up * 0.05f);
		}
		
		if (dirPlat	 == 1)
		{
			transform.Translate(Vector3.down * 0.05f);
		}
		
	}
}

the fameCount max number will dictate the distance between point A and B in frames, works really nice (just apply the script to the elevator object/flat cube) and the dirPlat (public so you can easily change in unity) will dictate witch direction it will go first (so if it starts at the bottom you start with 0, if it starts at the top you start with 1)

my only problem is that I’m trying to make it so it will stop for 2 secs (120 frames) after reaching top destination and bottom destination. atm its just going up and down without stoping so its hard for the character to jump in… any ideas?

Thank You! This was extremely helpful as I am making a VR playground app that needed an elevator! :slight_smile: