Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by CalvinAMi · Jun 05, 2015 at 04:11 PM · pausewaitforsecondsdelaymoving platformmoving-platform

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;
         }
     }
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CalvinAMi · Jun 12, 2015 at 09:39 AM 0
Share

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. =\

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
          }
      }
  }

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CalvinAMi · Jun 12, 2015 at 02:25 PM 0
Share

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 =)

avatar image MechanicalGaming · Jun 12, 2015 at 10:51 PM 1
Share

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.

avatar image CalvinAMi · Jun 13, 2015 at 05:33 PM 0
Share

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;
         }
     }
 }
avatar image MechanicalGaming · Jun 13, 2015 at 10:47 PM 0
Share

Your welcome mate! I'm happy to help.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges