hi!
i made a script to move a platform from point to poin, but i’d like it to stop for X seconds in each point.
the object where the script goes has an empty object as patent, the actual platform as the first child and N empty game objects that are used to set the points where the platform have to go. it works perfectly, but doen’t stops when reaches a point and goes immediately to the next.
here is the code:
`using UnityEngine;
using System.Collections;
public class Platform : MonoBehaviour {
public bool bTurnOnOff = false;
public float fSpeed = 2.0f;
public float fWaitTime = 5.0f;
private int n;
void Start () {
n = 2;
}
void Update () {
if (bTurnOnOff == true) {
transform.GetChild(0).localPosition = Vector3.MoveTowards (transform.GetChild(0).localPosition, transform.GetChild(n).localPosition, fSpeed * Time.deltaTime);
ChangePos ();
}
}
void ChangePos () {
if (transform.GetChild(0).localPosition == transform.GetChild(n).localPosition){
if (n <= transform.childCount-1){
n = n + 1;
}
if (n > transform.childCount-1){
n = 1;
}
StartCoroutine (Wait());
}
}
IEnumerator Wait () {
yield return new WaitForSeconds (fWaitTime);
}
}
`
what am i doing wrong?
i'm creating a platform that will move from one point to the other (something between 2 and infinite points) to transport the player and rigid body objects, but i whant the platform to stop in each point so the player can come in easily i'll try what you sugested thank you!
– Simpson95I'll give you a hand with it when I get home tonight. Maybe someone else could offer a solution before that. Either way , what you're asking is pretty trivial. If you want to try your hand at it, this is what you need: - a list of waypoints - A script that moves the platform at a certain speed or over a given time (which do you prefer?), with a check that will detect when the platform is at the end of the line and begin reversing back over it if it doesn't loop. - A platform that childs the player to it when they enter a trigger, and unchilds it when they leave.
– iwaldropi didn't understand the line 4. could you explain? i tried EXACTLY what you posted and it doesn't work. correcting the variable "waitTime" and using your general idea with my code also did'n work
– Simpson95You mean the ternary conditional? Do you know how to use Google? Look it up! :) And the code I posted won't work for two reasons. 1) 'n' is not defined in the method. (It shouldn't even compile) 2) nothing happens a after the yield It was just an example. Also, I bet you're not calling it using the StartCoroutine method.
– iwaldrop