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!
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?
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.
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);
}
}
}