- Home /
Moving Platform with delay not working
I about to go nuts trying to figure out why my MovingPlatform script won't actually pause/delay.
I get no errors, and my Debug.Log info executes correctly, but my platform is NOT delaying when moving from one Waypoint to another.
What am I missing?
***HELP PART 2: I want to move the "delayTimer" and actually add it to each Transform that is created from the line "public Transform[] Waypoints;" Is there a way to do that? That way, the user could add a delay to some waypoints, and leave some at 0.
ANYWAY.... Thank you for looking and helping me understand where my brain is failing me =)
using UnityEngine;
using System.Collections;
public class MovingPlatform : MonoBehaviour
{
public Transform[] Waypoints;
public float moveSpeed = 3;
public float rotateSpeed = 0.5f;
public float scaleSpeed = 0.5f;
public float delayTimer = 3;
public int CurrentPoint = 0;
IEnumerator timeToDelay()
{
Debug.Log("Delay timer activated...");
yield return new WaitForSeconds(delayTimer);
Debug.Log("Delay timer completed!");
}
void FixedUpdate ()
{
if (transform.position != Waypoints [CurrentPoint].transform.position)
{
transform.position = Vector3.MoveTowards (transform.position, Waypoints [CurrentPoint].transform.position, moveSpeed * Time.deltaTime);
transform.rotation = Quaternion.Lerp (transform.rotation, Waypoints [CurrentPoint].transform.rotation, rotateSpeed * Time.deltaTime);
transform.localScale = Vector3.Lerp (transform.localScale, Waypoints [CurrentPoint].transform.localScale, scaleSpeed * Time.deltaTime);
}
if (transform.position == Waypoints [CurrentPoint].transform.position)
{
StartCoroutine(timeToDelay());
CurrentPoint += 1;
}
if( CurrentPoint >= Waypoints.Length)
{
CurrentPoint = 0;
}
}
}
Please. Can someone with the know-how take a look at this and offer some insight? Or.... if there is a better script to use, can I get a link to it? I am trying to move past this issue in my game's development but I really need this functionality so I can start the alpha test phase of my game. =\
Answer by MechanicalGaming · Jun 12, 2015 at 10:25 AM
Instead of using IEnumerator try setting up your own wait (at least that's what I always do).
For example this is one of my waits for jumping:
Hope it helps
if(Input.GetButtonDown("Jump") && jumpdelay < Time.time)
{
Invoke("Jump", 0f);
jumpdelay = Time.time + 0.6f;
}
P.S. 0.6f is in seconds.
I've decided to edit your code because it might be a bit complicating so put this:
There may be a couple of typing errors but they'll be easy to correct.
using UnityEngine;
using System.Collections;
public class MovingPlatform : MonoBehaviour
{
public Transform[] Waypoints;
public float moveSpeed = 3;
public float rotateSpeed = 0.5f;
public float scaleSpeed = 0.5f;
public float delayTimer = 3;
public int CurrentPoint = 0;
//New Float
public float delay;
void FixedUpdate ()
{
if (transform.position == Waypoints [CurrentPoint].transform.position)
{
delay = Time.time + delayTimer;
} else if (transform.position != Waypoints [CurrentPoint].transform.position && delay < Time.time)
{
transform.position = Vector3.MoveTowards (transform.position, Waypoints [CurrentPoint].transform.position, moveSpeed * Time.deltaTime);
transform.rotation = Quaternion.Lerp (transform.rotation, Waypoints [CurrentPoint].transform.rotation, rotateSpeed * Time.deltaTime);
transform.localScale = Vector3.Lerp (transform.localScale, Waypoints [CurrentPoint].transform.localScale, scaleSpeed * Time.deltaTime);
}
if( CurrentPoint >= Waypoints.Length)
{
CurrentPoint = 0;
}
}
}
Thank you VERY much for the help $$anonymous$$echanicalGa$$anonymous$$g !!! When I use the edited code you supplied, the "Delay" timer just counts up from 0 infinitely =\ I am headed out on holiday for a few days. I'll take my laptop along with me to see if I can tweak it to work =) I am so close! Thanks again =)
Your welcome! Happy to lend a hand and try using the delay > Time.time on the first if... that might give you an idea of what to work on.
I GOT IT!!! $$anonymous$$echanicalGa$$anonymous$$g, you absolutely rock! I can't thank you enough...
All I was missing was to tell the script to move to the next "CurrentPoint" after the delay.
I'm still not sure why my use of WaitForSeconds wouldn't work correctly, but who cares =)
$$anonymous$$aybe I'll figure that part out in the future and be that much better off.
I still need to figure out "HELP PART 2" in original post
For now, I can move beyond this hiccup and get on with development =) Thanks a million!
BELOW IS $$anonymous$$Y CO$$anonymous$$PLETE CODE FOR A SI$$anonymous$$PLE $$anonymous$$ovingPlatform WITH PAUSES =)
using UnityEngine;
using System.Collections;
public class $$anonymous$$ovingPlatform : $$anonymous$$onoBehaviour
{
public Transform[] Waypoints;
public float moveSpeed = 3;
public float rotateSpeed = 0.5f;
public float scaleSpeed = 0.5f;
public float delayTimer = 3;
public int CurrentPoint = 0;
float delay;
void Update ()
{
if (transform.position == Waypoints [CurrentPoint].transform.position)
{
delay = Time.time + delayTimer;
CurrentPoint += 1;
}
else if (transform.position != Waypoints [CurrentPoint].transform.position && delay < Time.time)
{
transform.position = Vector3.$$anonymous$$oveTowards (transform.position, Waypoints [CurrentPoint].transform.position, moveSpeed * Time.deltaTime);
transform.rotation = Quaternion.Lerp (transform.rotation, Waypoints [CurrentPoint].transform.rotation, rotateSpeed * Time.deltaTime);
transform.localScale = Vector3.Lerp (transform.localScale, Waypoints [CurrentPoint].transform.localScale, scaleSpeed * Time.deltaTime);
}
if( CurrentPoint >= Waypoints.Length)
{
CurrentPoint = 0;
}
}
}
Your answer
Follow this Question
Related Questions
Loop Animation with delay-variable 1 Answer
How to PAUSE A game for perticular time 3 Answers
WaitForSeconds, only pausing once 1 Answer
Can't get platform to move to the other direction 2 Answers
check alpha of gameObject C# 3 Answers