- Home /
[ANSWERED]Move object without Update or coroutine
Ok so i am aware there are a number of ways to smooth move an object from point A to point B either through the Update() or using a coroutine. But as for discovering a way to do this without either of these is proving to be somewhat problematic. I came across this but could not get it working at all. Usually I would use a coroutine but in this case my specifications state otherwise. Does anyone out there have a solution or a link to a script reference I can investigate?
well you have this function
I don't know what kind of specifications you have but not using update() or coroutines to move your objects is weird.
Understanding the problem you are trying to solve might help us direct you to an answer. I agree with @Nerevar that what you are trying to do is strange on the surface. You need to cause something to execute regularly:
FixedUpdate()
LateUpdate()
On the Repaint type in OnGUI() (not recommended)
InvokeRepeating() (as suggested by @Nerevar)
Chained Invoke (not recommended)
When the user swipes a direction, a GameObject moves to a specific location depending on the direction of the swipe, which i want it to move there over time. To my understanding as it waits on user input to be called I cant use update. I have been instructed by the project lead to avoid using Coroutines which is how I would usually execute something like this.
I will try the Invoke Repeating $$anonymous$$ethod that Nerevar suggested. I would not get away with using an animation as there are 4 different animations from each original position and 9 original positions. To many animation files.
The reason im instructed to avoid coroutines is that i am told they can be unreliable with large projects and can cause more issues than they solve. Do you know of evidence to support this? in my own work i use them fairly often with few problems?
Ok yeah I get you. Had a bit of a brain fart. I used a enum state $$anonymous$$achine and checked it within the update for movement and got it sorted now. Just as a side point, I assume that using an enum or state machine to constantly check within the update would be more costly than Coroutines? From what I can see there doesn't seem to be many drawbacks to coroutines
oo and thanks for the helps
Update functions are events, events that are always triggered if they exist in the script (check the docs on execution order). Even if your Update function is empty, it will always execute every frame (generating some unnecesary overhead, very small overhead, but overhead after all). Coroutines controlled by yield instructions can also be executed every frame, same as updates. The good thing about coroutines, is that once they finish, they won't bother anymore (unlike the Update function that will stay there forever, even if it doesn't do anyting anymore).
Of course, there are some variations, like FixedUpdate function that executes whenever you want, and yield WaitForSeconds that allows you to manage its execution.
I'm not saying that you shouldn't use Update to check your state machine and perform the movement (actually that's not a bad idea after all), because that is not a too heavy of an operation. What you shoud try to avoid is using heavy operations like GameObject.Find() in your Update. Just remember that Update triggers every frame, so be gentle with it.
Answer by Andres-Fernandez · Jun 02, 2014 at 03:59 PM
There's no reason you can't use Update. You just need some flag to trigger the movement (and the flag being set by the swipe made by the user). Inside your update you can check the flag, if it's true, then move. The end of the movement sets it back to false.
It's a problem of the logic used to trigger the movement, not the method used to move the object.
Answer by Andres-Fernandez · Jun 02, 2014 at 01:32 PM
What about an animation? If you know point A and point B you can just create it procedurally by code.
[EDIT] I've done it before, all you need to check is AnimationClip , SetCurve and AnimationCurve in the docs.