# Making a platform rise and fall?

Hey everyone, I am new to Javascript, so I am still learning. I am trying to figure out how to script a platform and make it rise up, then pause for 3 seconds at a certain point, then go back down again, and pause for another 3 seconds. I have a little trouble understanding vectors, so if someone could explain the code for this to happen would be very appreciated. Thanks!

-Blayke

``````var height:float = 5.0;
var up = true;
var speed = 1.0;
var delayTime = 3.0;
private var pos1:Vector3;
private var pos2:Vector3;

function Start()
{
pos1 = transform.position;
pos2 = transform.position + Vector3(0,height,0);
if(!up)
transform.position = pos2;

StartCoroutine("Move");
}

function Move()
{
while(true)
{
var direction:Vector3;

if( up )
{
direction = Vector3(0,speed,0);
if( rigidbody.position.y > pos2.y )
{
up = false;
yield WaitForSeconds( delayTime );
}
}
else
{
direction = Vector3(0,-speed,0);
if( rigidbody.position.y < pos1.y )
{
up = true;
yield WaitForSeconds( delayTime );
}
}

rigidbody.MovePosition( rigidbody.position + direction * Time.deltaTime);
yield;
}
}

function OnDrawGizmos()
{
if( !Application.isPlaying )
Gizmos.DrawLine( transform.position, transform.position+Vector3(0,height,0) );
else
Gizmos.DrawLine( pos1, pos2 );
}
``````

Component => Physics => Rigidbody
2.Check “Is Kinematic” and uncheck “Use Gravity”
That`s all!

Thank you so much! That worked perfectly! Thanks

I’m going to try this as soon as I’m behind my comp.
Tips like this are awesome, just the script et voilá.

I need stuff like defining states and setting a cooldown, hopefully I can use it do do a lot more.

Thanks!

Awesome; just what I need for a little platformer project. Thanks!

The script works fine – until I step on the platform! Then when the platform moves up, I stay where I am until it passes, at which point I fall to my death

How to make the camera move with the platform? I’m using the First Person Controller prefab, in it’s default configuration.

The platform that’s being moved is part of a level imported from Maya with Generate Colliders ticked; I can move and jump on the rest of the level fine.

With a CharacterController, you simply have to move the character by the same amount as the platform, since it doesn’t have physics as such. You don’t actually need to do this when the platform is falling, but there’s no problem if you do it anyway.

I guess the name “Coldcity” is appropriate for Cumbria today, eh?

andeeee, you’re not wrong re the cold – brrr. I don’t really mind, I’ve plenty of fuel and food and I’m on the correct side of the window

Thanks for the hint. So far I’ve:

• Set the moving platform’s Mesh Collider isTrigger = on
• Added the following to the moving platform script:
``````function OnTriggerEnter (other : Collider) {
other.transform.parent = gameObject.transform;
print ("Player on platform");
}

function OnTriggerExit (other : Collider) {
other.transform.parent = null;
print ("Player left platform");
}
``````

Not having any luck yet but I’ll keep playing. I see the “Player on platform”, “Player left platform” messages but still just fall through.

Hmm Still not getting anywhere… go on, give us another clue

Got it now

``````function OnTriggerEnter (other : Collider) {
if (other.gameObject.CompareTag ("Player")) {
var comp = other.GetComponent(FPSWalker);
comp.activePlatform = gameObject;
print ("Player on platform");
}
}

function OnTriggerExit (other : Collider) {
if (other.gameObject.CompareTag ("Player")) {
var comp = other.GetComponent(FPSWalker);
comp.activePlatform = null;
print ("Player left platform");
}
}
``````

… Then my FPSWalker script has an activePlatform var, which it uses when doing it’s movement (this technique gleaned from the 2D platform tute).

Thanks again

just how did you get this to work? been trying to puzzle it out for a few days now. have the first script on a moving platform of my own and the character could stand on it just fine but once it started moving the character fell through. tried implementing the rest of what was given but now the collider doesnt even interact with the character.

mine just gives me an error is says

Assets/platformMove.cs(1,11): error CS8025: Parsing error

An easier solution, more optimal code wise, complete with comments to so easier to understand what’s going on:

``````//PlatformRise.js
//Javascript

public var distance : float;    //how far to move up from starting position
public var moveTime : float;    //time it takes to move from A to B in seconds
public var delay : float;        //how long to wait at each destination point

private var _transform : Transform;    //cache to our transform component
private var _startPos : Vector3;    //cache to our starting position
private var _endPos : Vector3;        //cache to our ending position
private var _timer : float;            //used to count seconds
private var _waiting : boolean;        //whether we're waiting or moving
private var _state : boolean;        //are we moving up or down (false = up, true = down)

//use this to initialize component references and variables
function Awake()
{
_transform = transform;
_startPos = _transform.position;
_endPos = _startPos + (new Vector3(0, distance, 0));
_state = false;
_waiting = true;
}

//use this to initialize components and script references
function Start()
{

}

//this runs every frame
function Update()
{
_timer += Time.deltaTime;
if (_waiting)
{//we're currently waiting
if (_timer >= delay)
{//count seconds until we elapse delay amount of seconds
_timer -= delay;        //reset timer
_waiting = false;        //we're done waiting
}
}
else
{//we're currently moving
if (_timer >= moveTime)
{//count seconds until we elapse moveTime amount of seconds
_timer -= moveTime;        //reset timer
_state = !_state;        //flip state (!true = false) & (!false = true);
_waiting = true;        //we now wait
}
if (_state)
{//we're moving down
_transform.position = Vector3.Lerp(_endPos, _startPos, _timer / moveTime);
}
else
{//we're moving up
_transform.position = Vector3.Lerp(_startPos, _endPos, _timer / moveTime);
}
}
}
``````