- Home /
Moving Platform
in my game I want to make a platform (cube) move between two objects, bouncing back and forth at at all times so my first person controller can get on it and move across to a different set of land can i please have a script to make it do that i havent figured one out yet. Thank you
Sorry to bump an old thread but I've been testing out this moving platform code and for the most part it works perfect.
There is one issue with this code though. When you exit the trigger that is attached to the moving platform, any child objects of the parent that have a box collider do not return with the parent/player object.
While you can still move around and have normal controls as usual, the box collider child objects still remain stuck as children of the moving platform.
Is there a way to resolve that?
Answer by aldonaletto · Jun 16, 2012 at 12:44 AM
Just make a script to move the platform - the First Person Controller prefab already can ride moving platforms: if you jump over a moving platform, the FPC will move with it as you would expect.
There are several alternatives to create a moving platform - this is one of the easiest ways:
var amplitude: float = 5; // platform excursion (+/- 5 units, in this case)
var speed: float = 0.2; // movements per second
var direction: Vector3 = Vector3.forward; // direction relative to the platform
private var p0: Vector3;
function Start(){
p0 = transform.position;
while (true){
// convert direction to local space
var dir = transform.TransformDirection(direction);
// set platform position:
transform.position = p0+amplitude*dir*Mathf.Sin(6.28*speed*Time.time);
yield; // let Unity free till the next frame
}
}
Create a Unity's cube, adjust its dimensions and add this script to it, then tweak the parameters speed, amplitude and direction - the last one is a vector that defines the movement direction relative to the platform.
it says "Assets/Scripts/$$anonymous$$oving_Platform.js(12,48): BCE0017: The best overload for the method 'UnityEngine.$$anonymous$$athf.Sin(float)' is not compatible with the argument list '(UnityEngine.Vector3)'." and "Assets/Scripts/$$anonymous$$oving_Platform.js(12,28): BCE0051: Operator '+' cannot be used with a left hand side of type 'UnityEngine.Vector3' and a right hand side of type 'float'. "
fixed the error but it doesnt work, u said that there are more than one way. can i have another? thanks for the help
I was almost sleeping over the keyboard, and made some stupid mistakes - that's the right instruction:
transform.position = p0 + amplitude * dir * $$anonymous$$athf.Sin(6.28 * speed * Time.time);
Answer fixed!
thank you it works now-- i have moving platforms and elevators thanks to you! thanks
I dont understand why but the First Person Character doesnt moves with the platform. For example when the things moves forward and backwards i have to use the controls (press w or s) to go with it, otherwise i fall.On a platform which is moving on y coords, things are diffeent. In the first step i "feel" the collission, but as soon as it moves up i glitch trough the platform and stay on the floor. Any suggestions?
Btw thanks a lot for the script!
Answer by Berenger · Jun 16, 2012 at 12:56 AM
If you don't like iTween, use an AnimationCurve. The result of Evaluate will be between 0 an 1, you can use it in a Lerp.
Answer by rubenpvargas · Jun 16, 2012 at 12:14 AM
Use itween, it´s free :) http://itween.pixelplacement.com/index.php
no cant you just give me a script i dont like itween
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Collision with specific object... 2 Answers
Detection if a GameObject is below you or next to you? 1 Answer
Melee-like function? 1 Answer