I would like to apply a constant-speed everlasting translation to a game object (e.g a background) in Unity. Without physics.

What is the best way to achieve that?

  • \$\begingroup\$ What did you try? Did you try anything at all? Please refer to our FAQ on writing proper questions. \$\endgroup\$ – K.L. Mar 16 '15 at 10:44
  • \$\begingroup\$ Where is this FAQ, please? \$\endgroup\$ – sdabet Mar 16 '15 at 10:52
  • \$\begingroup\$ gamedev.stackexchange.com/help/asking here you go :) \$\endgroup\$ – K.L. Mar 16 '15 at 10:55
  • \$\begingroup\$ Thanks. I haven't tried anything yet. I was wondering what was the best practice to achieve this common problem in Unity. But maybe it's not the appropriate stackexchange site for that. Sorry if it's the case. \$\endgroup\$ – sdabet Mar 16 '15 at 11:04

There is no one "best way" and what is best is subjective.

The method I present is quite simple - just change the position over time. Use Time.deltaTime so the motion seems fluid regardless of the length of the frame. You can substitute for Vector3.forward with any other vector with the magnitude of 1 - it decides in what direction does your object move.

I dont have access to Unity right now to test it, but the script would look similar to this:

float speed = 1.0f;

public void Update()
  Vector3 newPosition = transform.position + (Vector3.forward * speed * Time.deltaTime);
  transform.position = newPosition;
| improve this answer | |
  • \$\begingroup\$ That sounds good. Thanks. Of course when I say "best way" I mean "the simplest and most obvious way which would commonly be considered as a best practice" (but I thought it would just make the question more confuse...) \$\endgroup\$ – sdabet Mar 16 '15 at 10:58
  • 1
    \$\begingroup\$ This is pretty much it. Translating some direction at speed X and accounting for time passed is as simple as it gets. There are other ways (animations w root motion, using physics and applying force, etc). But those are generally going pointlessly out of the way for something very simple. \$\endgroup\$ – DrHeinous Mar 16 '15 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.