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 RayJr · Oct 27, 2014 at 02:36 AM · 2dtransformtime.deltatime

Simple Transform Move

Hi, I'm trying to control an object moving up slowly. Right now i'm using a key stroke, but eventually, I want to just call this method from script.

The following code works fine and the object moves up slowly. (For some reason it starts moving faster - but thats not my problem).

 using UnityEngine;
 using System.Collections;
 
 public class WaveConroller : MonoBehaviour {
 
     void Start () {
     
     }
 
     void Update () {
         
         transform.Translate (Vector2.up * Time.deltaTime );
         
 
     }
 }

However, when I move this code to a method and call it using a keystroke, it happens instantaneously. If i increase my loop to something like t<50, and press the key, it waits, then moves the object way up instantly. As below.

 using UnityEngine;
 using System.Collections;
 
 public class WaveConroller : MonoBehaviour {
     private float t;
 
     void Start () {
     
     }
 
     void Update () {
         bool Up = Input.GetButtonDown ("Up");
         transform.Translate (Vector2.up * Time.deltaTime );
         if (Up)
             MoveUp();
 
     }
 
     void MoveUp ()
     {
         t = 0f;
         while (t<1)
         {
             t += Time.deltaTime;
             transform.Translate (Vector2.up * Time.deltaTime );
             Debug.Log (t);
         }
 
         
     }
 }
 
 
 Any Ideas? 
 
 Thanks!

 
Comment
Add comment · Show 5
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 Pyrian · Oct 27, 2014 at 12:31 PM 0
Share

Are you aware that the entire while loop is occurring inside a single Update call?

avatar image RayJr · Oct 27, 2014 at 12:52 PM 0
Share

How so? Its being called from update, but why wouldn't it have a chance to fininsh on its own?

Are you saying that everything that is called from Update needs to be completed all within the duration of that single update?

That seems strange to me. How would you ever call something by keystoke that takes more then a fraction of a second?

avatar image MrSoad · Oct 27, 2014 at 01:09 PM 1
Share

The two answers below by Yword and Sarper Soher are both good answers. To clarify, you are right when you say that "Are you saying that everything that is called from Update needs to be completed all within the duration of that single update", yes it does.

Yword shows you how to avoid this via the use of a coroutine, which will run alongside Update rather than executing within it.

Sarper Soher shows you how to move it a little bit each frame without getting into loops inside Update issues, which is what you were doing in your first script, only without the keypress event.

Coroutines and Invokes are something well worth reading up on, they have various essential uses, Invokes can reduce processor usage significantly as you can specify how often you want it to run via InvokeRepeating. If something only needs to run two or three times a second rather than 60 ish(which it would in Update) then this will do that for you.

avatar image RayJr · Oct 27, 2014 at 01:24 PM 1
Share

$$anonymous$$r Soad, you know how when you get in the car and its cold outside and the rear window fogs up, then you press the rear defrost button and it clears up. You're the rear defroster of Unity.

  • if you live in Florida or something where poeple don't use rear defrost buttons - just thanks for clearing things up as usual.**

avatar image RayJr · Oct 27, 2014 at 03:07 PM 0
Share

I think I'm going to switch to JS. This would have worked in JS as is from what I can tell. JS is also a lot close to what I use at work.

... Switched.

3 Replies

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

Answer by Yword · Oct 27, 2014 at 05:30 AM

I am not too sure about the problem... But is that you want to move the object up for some duration when the button Up is pressed? If so, then maybe you can try the following code:

 using UnityEngine;
 using System.Collections;
 
 public class WaveController : MonoBehaviour
 {
     private bool isMovingUp = false;
 
 
     private void Update() 
     {
         bool isInputUp = Input.GetKeyDown(KeyCode.UpArrow);
 
         if (isInputUp && !isMovingUp)
         {
             StartCoroutine(MoveUp(1));
         }
     }
 
 
     private IEnumerator MoveUp(float duration)
     {
         isMovingUp = true;
 
         float t = 0f;
 
         while (t < duration)
         {
             t += Time.deltaTime;
 
             transform.Translate(Vector2.up * Time.deltaTime);
 
             yield return null;
         }
 
         isMovingUp = false;
     }
 }
 
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
1

Answer by SarperS · Oct 27, 2014 at 05:32 AM

 using UnityEngine;
 
 internal sealed class MoveUp : MonoBehaviour {
     [SerializeField] private float speed = 1;
 
     private void Update() {
         if(Input.GetKey(KeyCode.UpArrow)) MoveInAxis(Vector3.up, Space.World);
     }
 
     private void MoveInAxis(Vector3 axis, Space space) {
         transform.Translate(axis * speed * Time.deltaTime, space);
     }
 }
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 YasanthaPrabath · Oct 27, 2014 at 04:19 PM

When you call MoveUp() method from Update, Update don't wait till MoveUp(); finish. Its just simply go to next Update.

In next update, if you keep pressing Up button, its again getting called( MoveUp() method ) so there is 2 MoveUp() methods are running in memory, Since your while loop looking at t < 1 AND update is called nearly 60 times in Update if you kept press Up key 1 second there is 60 MoveUp() methods are executing in memory.

If I were you i will try something like following,

     bool isRunning = false;
     void Update()
     {
       if( isRunning )
          return;
 
       if( Input.GetKey(KeyCode.UpArrow) )
       {
           isRunning = true;
           MoveUp();
       }
    }
 
      void MoveUp ()
      {
          t = 0f;
          while (t<1)
          {
              t += Time.deltaTime;
              transform.Translate (Vector2.up * Time.deltaTime );
              Debug.Log (t);
          }
         isRunning = false;
      }


But my all time fav is Using in Coroutine

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.

Follow this Question

Answers Answers and Comments

31 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Sprite moving too far/fast 1 Answer

moving objects with transform position 2 Answers

An easy way to get the global position from a child object 5 Answers

Problem when rotating sprite and moving it foward 1 Answer

Make 2D Ball sprite spin? 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