• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by DedValve · Sep 21, 2014 at 05:32 PM · pause

How to make a moving object temporarily pause?

Hi All, I'm trying to make a simple cube move at a set speed, pause when it reaches a certain point on its x-axis and then continue moving again. I'm trying to do yield wait for seconds but I could be very wrong, after searching the web I can't seem to find out what I'm missing or doing wrong.

Perhaps an invoke function (I'm not familiar with those) would be better?

Here's my code:

 var amtToMove: float; //variable to edit the speed of the moving object 
 var enemySpeed: int;
 var moveforward: boolean;
 
 function Start(){
     moveforward = true;
 }
 
 function Update () {
     //Checks to see which operation to perform - move or stop
     if(moveforward)
         move();
     else 
         stop();
 }
 
 
 function move(){
 
     amtToMove = enemySpeed * Time.deltaTime;
     
     transform.Translate(Vector3(-1, 0, 0) * amtToMove);
     
     return;
 
 }
 
 function stop(){
 
     if(transform.position.x <= -8){
     
         Debug.Log("stop point");
     
          yield WaitForSeconds(5);
          
          moveforward = false;
          
          return; 
          
     }
     
 }

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Slev · Sep 21, 2014 at 05:42 PM

I think you just have your ordering wrong. I think you just want:

 function Update() {
   if (moveforward)
     move();
   else
     stop();
 }

 function move() {
   if (tranform.position.x <= 8)
     moveforward = false;
   else {
     amtToMove = enemySpeed * Time.deltaTime;
 
     transform.Translate(Vector3(-1, 0, 0) * amtToMove);
 
     return;
   }
 }

 function stop() {
   Debug.Log ("Stop point");

   yield WaitForSeconds(5);

   moveforward = true;
   return;
 }

Edit: more to fix...

I believe your problem is when variables are being set and changed. Based on the description I changed where your flags are being set so it's more logical.

Comment
Add comment · 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
0

Answer by Ferb · Sep 21, 2014 at 05:58 PM

As far as I know, 'yield' only works in a coroutine.

You can use the Invoke function to call a function after a set time though.

You might want something like this:

 function Pause(){
     if(!moveforward) return;
     moveforward = false;
     Invoke("Unpause",5);
 }
 function UnPause(){
     moveforward = true;
 }

Bear in mind also, that at the moment you only test the position in the stop function. It won't know to go to the stop function until it's tested what position it's in, and is won't test what position it's in until it's gone there. So you have to test the position in the update function or the move function. Remember as well that after it's stopped for a few seconds and is ready to start again, it will still have a position less than -8, and so will immediately pause again, and so will keep pausing and effectively never start, unless you add a variable to check whether you've already paused once, so you know not to pause again.

Comment
Add comment · Show 1 · 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 Slev · Sep 21, 2014 at 06:40 PM 0
Share

@Ferb In UnityScript any function containing a yield statement is understood to be a coroutine and is automatically "invoked" when called.

avatar image
0

Answer by DedValve · Sep 21, 2014 at 10:38 PM

thanks for the answers, @slev's answer worked but once it gets to the desired location and pauses it doesn't move forward again. As you said @ferb a var boolean might do the trick.

I'm thinking I put the false statement in function stop and the true statement inside the else statement of function move. But I'm not sure how to approach this exactly, am I on the right track or am I completely misunderstanding how vars work?

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Pause-Resume one button using switch statements? 1 Answer

comend for pausing 1 Answer

Score wont stop after game is paused 3 Answers

How can i pause gradually from timeScale 1.0 to 0.0 over a second? 1 Answer

How do I pause and unpause multi gameobjects in the scene for a five seconds 1 Answer


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