- Home /
How to trigger a Moving Platform with a collider?
Hey, So I'm making a 2.5D sidescroller and there is a section where i need a lift to start moving down when the player stands on it.
Ive used the moving platform script straight from the 2D sidescroller tutorial and it works perfectly fine on its own, i just need a method of stopping it from running until the player gets there, is this possible?
Im also using the standard 'ActivateTrigger' script straight from the general scripts.
Please Help ASAP, I'm sure its a simple solution I'm just new to Javascript and Unity as a whole and can't seem to figure it out for myself.
All answers appreciated.
Thanks
Answer by reptilebeats · May 30, 2012 at 05:19 PM
the most simplest solution i can think of is to make an animation so basically just animate it moving from the top to bottom and turn loop off and start on play off.
then put a script on a collider that says
function OnCollisionEnter(col : Collision){
if(col.gameObject == gameObject.Find("your character")){
animation.Play();// if animation isnt on this then you will have to find it
enabled = false;
}
}
Answer by aldonaletto · May 31, 2012 at 08:49 AM
You should use a different script to control the platform - MovingPlatform.js relies on the current Time.time to set the platform position, thus it can't be stopped. A good solution is to use a coroutine to do the job: once called, it runs in the background, moving the platform to the target point without any extra effort.
To control the movement, create a trigger volume: create a cube, child it to the platform, reset its position/rotation, then adjust its size and position to cover the platform. In the Inspector, set Is Trigger to make it a trigger, and clear the Mesh Renderer checkbox to make it invisible. Finally, add the script bellow to the trigger (not to the platform!):
var targetA : Transform; // drag here the empty game object that defines the start pos var targetB : Transform; // drag here the object that defines the end position var speed : float = 0.1; // speed (0.1 means 10s per movement)
private var curTarget: Transform;
function Start(){ // initialize platform at targetA position: curTarget = targetA; // set curTarget... // and move quickly the parent platform to its position (if not already there) MovePlatform(transform.parent, curTarget, 10); }
private var moving: boolean = false; // flag that warns when the platform is moving
function MovePlatform(platform: Transform, dest: Transform, vel: float){ moving = true; // set a flag to indicate "I'm moving" var from = platform.position; // get start position... var to = dest.position; // and end position... var t: float = 0; // initializes timer = 0 while (t < 1){ // cycle ends when timer reaches 1 t += Time.deltaTime vel; // advance timer at the desired speed... if (t > 1) t = 1; // and clamp it to 1 // then smooth it near to 0 and 1 with a cosine function: var weight = 0.5 (1 - Mathf.Cos(t * Mathf.Pi)); // set the platform position according to weight (0=from, 1=to) platform.transform.position = Vector3.Lerp(from, to, weight); yield WaitForFixedUpdate(); // let Unity free till the next fixed update } moving = false; // movement finished - clear the flag }
function OnTriggerEnter(other: Collider){ if (moving == false){ // if platform not already moving... if (curTarget == targetA){ // swap targets... curTarget = targetB; } else { curTarget = targetA; } // and move platform to the new target: MovePlatform(transform.parent, curTarget, speed); } } Use it like you were using MovingPlatform.js: drag to targetA and targetB the empty objects you've created to define the start and end positions. The platform starts at targetA position, and will move to targetB when any object enters its trigger volume. It will stand at targetB until any object enters the trigger again, what will make it go back to targetA position, and so on.
can this be modified to use say 4 different waypoints and do the same thing?