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
1
Question by eaj · Mar 16, 2013 at 11:39 PM · coroutinemovingwait

Move a gameobject smoothly without update, without coroutine

Hello,

Been searching for a while but all solutions for moving appear to use a coroutine. I too can successfully get an object to move to a point based on the position of a mouse click using a coroutine.

The problem I'm having is that a coroutine works outside of the update function, so the update can still do it's thing. Currently this means the user can click on the object, and other objects, to do stuff to the object, while the object is moving.

The behaviour I'm trying to achieve is make the game wait for the movement to complete before allowing the user input or before things can be done to the object or others.

I've tried not using a coroutine but the results of the movement are instantaneous. It doesn't provide linear movement. I've tried to use a variable as a means to store the objects state to busy, so as to prohibit other actions during this state, to no avail. The closest I've come to the busy variable solution is creating a cycle in the the moveLinear function, whereby I tried to set the Players state to busy in the moveLinear function but this created a cycle as the Player object calls moveLinear through a coroutine.

I may be going about this the wrong way. I'm new to game logic.

Here is the update code for the Player.

 function Update () {
     
     if (Input.GetMouseButtonDown (1)) {
         if (isSelected && common.getLocationFromTap() != null)
         {
             var goal : Vector3 = common.getLocationFromTap();
             StartCoroutine(move.moveLinear(object, goal, rateOfMovement));
         }
     }
 }

And the moveLinear code

 function moveLinear(object: GameObject, goal: Vector3, rateOfMovement: float)
 {
     while(true)
     {
         start = object.transform.position;
         if (start == goal)
             break;
         object.transform.position = Vector3.MoveTowards(start,goal,Time.deltaTime*rateOfMovement);
     }
 }
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

2 Replies

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

Answer by eaj · Mar 17, 2013 at 01:54 AM

Ah, of course!

I simply replaced the condition "if (!isBusy)" in the players update function with "if (!move.busy)" and it works!

Thanks for the help!

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 robertbu · Mar 16, 2013 at 11:44 PM

I cannot test it since I don't have all of your code, but this should work:

 #pragma strict
 
 private var bMoving = false;
 
 function Update () {
  
     if (!bMoving && Input.GetMouseButtonDown (1)) {
        if (isSelected && common.getLocationFromTap() != null)
        {
          var goal : Vector3 = common.getLocationFromTap();
             StartCoroutine(move.moveLinear(object, goal, rateOfMovement));
        }
     }
 }
 
 
 function moveLinear(object: GameObject, goal: Vector3, rateOfMovement: float)
 {
     bMoving = true;
     while(true)
     {
         start = object.transform.position;
         if (start == goal)
            break;
         object.transform.position = Vector3.MoveTowards(start,goal,Time.deltaTime*rateOfMovement);
     }
     bMoving = false;
 }
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 eaj · Mar 16, 2013 at 11:59 PM 0
Share

Hi, thanks for the prompt response!

The moveLinear function is a member of the movement script so it doesn't have access to the Players' b$$anonymous$$oving variable.

For clarity, the player script is called Player and the movement script is called $$anonymous$$oveController.

Here is the Player script

 #pragma strict
 
 var common: Common;
 var move: $$anonymous$$oveController;
 var object : GameObject;
 var isSelected: boolean = false;
 var isBusy: boolean = false;
 var rateOf$$anonymous$$ovement: float = 0.0001;
 var health: float = 100;
 
 function Awake () {
     object = gameObject;
 }
 
 function Update () {
     
     if (!isBusy)
     {
         // toggle selection
         if (Input.Get$$anonymous$$ouseButtonDown (0)) 
         {    
             if (common.getObjectNameFromTap() == object.name)
                 isSelected = true;
             else
                 isSelected = false;
         }
         // move selected object
         if (Input.Get$$anonymous$$ouseButtonDown (1)) 
         {
             if (isSelected && common.getLocationFromTap() != null)
             {
                 var goal : Vector3 = common.getLocationFromTap();
                 StartCoroutine(move.moveLinear(object, goal, rateOf$$anonymous$$ovement));
                 playerSelected(false);
             }
         }
     }
 }

And this is the movement script

 #pragma strict
 
 public class $$anonymous$$oveController {
     
     @HideInInspector
     var start: Vector3;
     var busy: boolean = false;
     
     function moveLinear(object: GameObject, goal: Vector3, rateOf$$anonymous$$ovement: float)
     {
         while(true)
         {
             start = object.transform.position;
             if (start == goal)
                 break;
             object.transform.position = Vector3.$$anonymous$$oveTowards(start, goal, Time.deltaTime*rateOf$$anonymous$$ovement);
             yield;
         }
     }
 }

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

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# yield not working. 2 Answers

Help with waiting / coroutine c# 1 Answer

Make a wait function 1 Answer

How to properly use Yield for a coroutine? 3 Answers

Wait inside method until user action 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