- Home /
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!
Are you aware that the entire while loop is occurring inside a single Update call?
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?
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.
$$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.**
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.
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;
}
}
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);
}
}
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
Your answer
Follow this Question
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