Javascript required
Skip to content Skip to sidebar Skip to footer

Slightly Continuing Movement of Object Unity 5

Moving an object in Unity can be very straightforward.

It typically involves modifying the properties of an object's Transform component, which is used to manage a game object's scale, rotation and position in the world.

However, there are many different ways you can do that…

And while most methods of moving an object in Unity involve modifying the object's Transform in some way, the exact method you use will vary depending on the type of movement you want to create.

But don't worry, because in this article, you'll learn how and when to use each of the different methods for moving an object in Unity, so that you can use the one that's right for your project.

Here's what you'll find on this page:

  • How to move an object in Unity
    • How to use Transform Translate in Unity
    • How to move an object with the keyboard in Unity
    • How to move an object, relative to the camera
  • How to move an object to a position in Unity
    • How to move an object to a position at a set speed (using Move Towards)
    • How to move an object to a position in a set time (using Lerp)
    • How to move an object to a position using animation
  • How to move an object using physics

Let's get started.

Moving Objects in Unity explained (video)

For a general overview of how to move objects in Unity try my video, or continue to the full article below.

How to move an object in Unity

The most straightforward method of changing an object's position in Unity is to set it directly, which will instantly move it to a new vector 3 position in the world.

This works by setting the Position property of an object's Transform component to a new position.

Like this:

          // Moves an object to the set position transform.position = new Vector3(10, 0, 5);        

Or you can add a vector to an object's position, to move it by a set amount in a specific direction.

Like this:

          // Moves an object up 2 units transform.position += new Vector3(0, 2, 0);        

Which looks like this:

Visualisation of instant movement in Unity

Adding a vector to an object's position moves it by that amount.

It's possible to modify the properties of an object's Transform from a script that's attached to it by using the Transform Property:

          Transform myTransform = transform;        

The Transform property, which is typed with a lower case 't', allows you to reference the Transform component of an object from a script that's attached to it, without getting a reference to it first.

Which is useful, as it allows you to easily reference, or change, an object's own position from a script.

Like this:

          Vector3 myPosition = transform.position;        

Modifying a Transform's position is the most straightforward method of creating movement in Unity.

Either by modifying it directly, over time, or by using the Translate function which allows you to move an object in a specific direction.

How to use Transform Translate in Unity

The Translate function in Unity moves an object by a set amount, relative to its current position and orientation.

Like this:

          void Start() {     // Moves the object forward two units.     transform.Translate(0,0,2); }        

This is different to simply adding a vector to the object's position, which would move it relative to world space, while Translate will, by default, move an object relative to its own local space.

This means that, when using Translate, passing in a forward vector will move the object in the direction of its blue axis, the Z-Axis.

When it's called once, Translate moves an object by a set amount one time.

However, by calling it every frame, it's possible to create continuous movement.

Like this:

          void Update() {     // Moves the object forward two units every frame.     transform.Translate(0,0,2); }        

However, there's a problem with this method.

Because framerates vary between devices and, because each frame often takes a different amount of time to process than the last, the speed of an object's movement would change depending on the framerate when moving an object in this way.

Instead, multiplying the movement amount by Time.deltaTime, which is the amount of time since the last frame, creates consistent movement that's measured in units per second.

Like this:

          void Update()  {     // Moves the object forward at two units per second.     transform.position = new Vector3(0,0,2) * Time.deltaTime;  }        

Which looks like this:

Visualisation of speed-based movement in Unity

Adding a scaled vector over time creates movement.

This works fine, however, it can sometimes be more convenient to manage movement direction and speed separately, by using a Unit Vector to determine the direction and a separate float value to control the speed.

Like this:

          public float speed = 2; void Update() {     // Moves the object forward at two units per second.     transform.Translate(Vector3.forward * speed * Time.deltaTime); }        

This has the same effect as manually typing out the vector, except that the speed is now separate and can easily be changed.

In this example, Vector3.forward is shorthand for (0,0,1) which is the forward direction in world space.

Multiplying Vector3.forward by the speed value, in this case 2, creates the vector (0,0,2) which, when multiplied by Delta Time, creates a forward movement of two units per second.

The result is the same as when manually typing out the vector, except that the speed is now separate and can easily be controlled.

Direction vectors in Unity

A normalised vector, or unit vector, in Unity is simply a vector with a length of 1 that describes a direction. For example the unit vector (0,0,1) describes the forward direction in world space.

Normalised vectors can be extremely useful for calculating distance and speed.

For example, you could multiply a unit vector by 10 to get a position that's ten units away in a certain direction.

Or you can multiply a vector by a speed value and by Delta Time to create movement at an exact speed in units per second.

While you can type unit vectors manually, you'll find a number of shorthand expressions for common directional vectors in the Vector 3 class.

For example:

            Vector3.forward   // (0,  0,  1) Vector3.back      // (0,  0,  -1) Vector3.up        // (0,  1,  0) Vector3.down      // (0,  -1, 0) Vector3.right     // (1,  0,  0) Vector3.left      // (-1, 0,  0)          

These relate to common directions in world space, however, it's also possible to get a normalised direction vector relative to an object's local position and rotation.

Like this:

            transform.forward  // object forward -transform.forward // object back transform.up       // object up -transform.up      // object down transform.right    // object right -transform.right   // object left          

These can be extremely useful for creating object relative movement.

Such as moving an object forward in the direction that it's facing.

Like this:

            public float speed = 2; void Update() {     // Moves an object forward, relative to its own rotation.     transform.position += transform.forward * speed * Time.deltaTime; }          

In this case, the object is still moving forward at a speed of two units per second.

The difference is that the direction is based on the object's orientation, not the world. Which means that if the object rotates, it'll turn as it moves.

However…

While using a Transform's local directions can be useful for getting the orientation of a specific object, some functions already operate in local space by default.

Such as the Translate function which, by default, applies movement relative to the object's local position and orientation, unless you set otherwise.

Which means that, if you use transform.forward with the Translate function, you'll get a compounded effect, where turning the object throws off its forward vector.

Creating object-relative movement in this way can be useful for adding player controls, where the player can move an object forward, backwards or sideways using input axes, such as from the keyboard.

How to move an object with the keyboard in Unity

To move an object with the keyboard, or with any other input device, simply multiply the direction of movement you want to apply, such as forward, for example, by the Input Axis you want to use to control it.

In Unity, when using the default Input Manager, you'll find an Input Axis for Horizontal and Vertical movement already set up and mapped to the WASD keys and arrow keys on the keyboard.

  • Input in Unity made easy (a complete guide to the new system)

Each axis returns a value between -1 and 1, which means that you can use the value that's returned to create a movement vector.

Like this:

          public float speed = 2; void Update() {     float x = Input.GetAxis("Horizontal");     float z = Input.GetAxis("Vertical");     Vector3 movement = new Vector3(x, 0, z);     transform.Translate(movement * speed * Time.deltaTime); }        

This allows you to create object-relative movement controls using the keyboard or any other input device.

Which looks like this:

Visualisation of object relative keyboard movement in Unity

The Horizontal and Vertical input axes can be used to create movement control.

Even movement controls with Clamp Magnitude

When moving an object using horizontal and vertical axes it's possible to create diagonal movement that is faster than the speed of movement of a single axis.

This is because the length of the vector that's created from a forward vector of 1 and a sideways vector of 1 is longer than either one on their own, at around 1.4.

Which means that holding forward and right on a keyboard, for example, would move the player 1.4 times faster than just moving forwards or sideways alone, causing uneven 8-way movement.

So how can you fix it?

One option is to normalise the vector, like this:

            Vector3 movement = new Vector3(x, 0, z).normalized;          

However, while this does work, it means that the length of the vector is always one, meaning that any value less than one, such as analogue movements, or the gradual acceleration that Unity applies to digital axis inputs, is always rounded up to one, no matter what.

This might not be a problem for your game if, for example, you're using the Raw Input values and are deliberately creating tight digital controls.

Otherwise, to also support values that are lower than 1, you can limit the length of the vector instead, using Clamp Magnitude.

Like this:

            public float speed = 2; void Update() {     float x = Input.GetAxis("Horizontal");     float z = Input.GetAxis("Vertical");     Vector3 movement = new Vector3(x, 0, z);     movement = Vector3.ClampMagnitude(movement, 1);     transform.Translate(movement * speed * Time.deltaTime); }          

This will limit the length of the vector to one, while leaving lower values intact, allowing you to create analogue 8-way movement that's even in all directions.

The Translate function creates movement that's relative to the Transform component that it's called from.

However, you may not always want to create player-relative movement.

So how can you move an object in a direction that's relative to a different object, such as the camera, for example?

How to move an object, relative to the camera

It's possible to move an object relative to the position of the camera in Unity by using the camera's forward vector in place of the object's forward vector.

Like this:

          public float speed = 2; void Update() {     Transform camTransform = Camera.main.transform;     Vector3 forwardMovement = camTransform.forward * Input.GetAxis("Vertical");     Vector3 horizontalMovement = camTransform.right * Input.GetAxis("Horizontal");     Vector3 movement = Vector3.ClampMagnitude(forwardMovement + horizontalMovement,1);     transform.Translate(movement * speed * Time.deltaTime, Space.World); }        

Because the movement is relative to the camera's position, the Relative To parameter in the Translate function needs to be set to World Space for this to work.

However, there's a problem…

The forward vector of the camera may, in some cases, be facing at an angle, down towards the player, meaning that any movement that's applied will also push downwards, instead of along a flat plane, as you might expect.

To fix this, you'll need to manually calculate the direction between the camera and the object, while leaving out any rotation that you want to ignore.

To calculate a direction, all you need to do is subtract the position of the origin from the position of the target and then, normalise the result.

Like this:

          Vector3 direction = (transform.position - Camera.main.transform.position).normalized;        

To ignore the difference in height between the camera and the object it's looking at, simply create a new Vector 3 that replaces the camera's height with the object's instead.

Like this:

          Transform camTransform = Camera.main.transform; Vector3 camPosition = new Vector3(     camTransform.position.x,      transform.position.y,      camTransform.position.z); Vector3 direction = (transform.position - camPosition).normalized;        

This will return a direction that is looking towards the object, but isn't looking up or down.

You can then use the corrected direction that's created to calculate movement that's relative to the camera on a flat plane.

Like this:

          public float speed = 2; void Update() {     Transform camTransform = Camera.main.transform;     Vector3 camPosition = new Vector3(camTransform.position.x, transform.position.y, camTransform.position.z);     Vector3 direction = (transform.position - camPosition).normalized;     Vector3 forwardMovement = direction * Input.GetAxis("Vertical");     Vector3 horizontalMovement = camTransform.right * Input.GetAxis("Horizontal");     Vector3 movement = Vector3.ClampMagnitude(forwardMovement + horizontalMovement, 1);     transform.Translate(movement * speed * Time.deltaTime, Space.World); }        

Which looks like this:

Visualisation of camera relative movement in Unity

You can create camera relative movement by using the camera's forward direction in place of the object's.

By understanding the relative direction of an object, it's possible to create any kind of movement control that you want.

However, a lot of the time, you may want to move an object in a different way, that's not directly controlled by the player.

For example, moving an object to a set position or towards another object.

How to move an object to a position in Unity

Generally speaking, there are two different ways to move an object into a specific position.

  1. By speed, where the object moves towards a target at a specific speed,
  2. By time, where the movement between the two points takes a specific amount of time to complete.

The method you use will depend on how you want to control the object's movement, by time or by speed.

Here's how both methods work…

How to move an object to a position at a set speed (using Move Towards)

It's possible to move an object towards another object or a specific position in the scene using the Move Towards function.

Move Towards is a function of the Vector 3 Class that will modify a Vector 3 value to move towards a target at a set speed without overshooting.

Which works great for moving an object towards a position in the world at a speed you can control.

Like this:

          public Vector3 targetPosition; public float speed=10; void Update() {     transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime); }        

The movement can also be smoothed, by using the Smooth Damp function, which eases the movement as it starts and ends.

Like this:

          public Vector3 targetPosition; public float smoothTime = 0.5f;  public float speed = 10; Vector3 velocity; void Update() {     transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime, speed); }        

This works by setting the position of the object to the Vector 3 result of the Smooth Damp function, passing in a target, the current position and a reference Vector 3 value, which the function uses to process the velocity of the object between frames.

This can be useful for smoothed continuous movement, where the target position may change from one moment to the next such as when following the player with a camera.

Such as in this example of a smooth camera follow script using Smooth Damp:

          public Transform player; public float cameraDistance = 5; public float cameraHeight = 3; public float smoothTime = 0.5f; public float speed = 10; Vector3 velocity; void Update() {     transform.LookAt(player.transform);     Vector3 offset = (Camera.main.transform.position - player.position).normalized * cameraDistance;     Vector3 targetPosition = player.position + offset;     targetPosition.y = cameraHeight;     transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime, speed); }        

In this example, the camera will smoothly move towards the player and turn to face them, while keeping at a height of 3 units and trying to stay at a distance of 5 units away.

Move Towards works great as a way to move an object towards a position dynamically.

This is because the movement is speed-based, and can be controlled even if the target position moves or is unknown.

However, there are many times where you may simply want to move an object to a known position over a set period of time.

Such as moving a platform, or opening a door.

So how so you create time-based movement in Unity?

How to move an object to a position in a set time (using Lerp)

Lerp, or Linear Interpolation, is used to find a value between a minimum and maximum based on a position value, 't', which is a float between zero and one.

The value that's returned depends on the value of t where, if t is 0 the minimum value is returned, while 1 returns the maximum value.

Any other value in-between 0 and 1 will return a representative value between the minimum and maximum ends of the scale.

          float lerpedValue = Mathf.Lerp(float minValue, float maxValue, float t);        

Typically, Lerp is used to change a value over a period of time, by incrementing t every frame to return a new value on the scale.

This can be used to change a colour, fade an audio source or move an object between two points.

  • The right way to use Lerp in Unity (with examples)

It works by passing in the amount of time that has elapsed during the Lerp, divided by the total duration. This returns a 0-1 float that can be used for the t value, and allows you to control the length of the Lerp movement by simply choosing how long you'd like it to take.

Like this:

          float timeElapsed; float lerpDuration = 3; float lerpedValue; void Update() {   if (timeElapsed < lerpDuration)   {     lerpedValue = Mathf.Lerp(0, 100, timeElapsed / lerpDuration);     timeElapsed += Time.deltaTime;   } }        

Vector3.Lerp works in the same way except that, instead of returning a float, it returns a point in the world between two others, based on the t value.

This can be useful for moving an object between two different positions, such as a door with an open and closed state.

Like this:

          public float openHeight = 4.5f; public float duration = 1; bool doorOpen; Vector3 closePosition; void Start() {     // Sets the first position of the door as it's closed position.     closePosition = transform.position; } void OperateDoor() {     StopAllCoroutines();     if (!doorOpen)     {         Vector3 openPosition = closePosition + Vector3.up * openHeight;         StartCoroutine(MoveDoor(openPosition));     }     else     {         StartCoroutine(MoveDoor(closePosition));     }     doorOpen = !doorOpen; } IEnumerator MoveDoor(Vector3 targetPosition) {     float timeElapsed = 0;     Vector3 startPosition = transform.position;     while (timeElapsed < duration)     {         transform.position = Vector3.Lerp(startPosition, targetPosition, timeElapsed / duration);         timeElapsed += Time.deltaTime;         yield return null;     }     transform.position = targetPosition; }        

In this example, I've used a coroutine to move the door object up by a set distance from the door's starting position, whenever the Operate Door function is called.

Which looks like this:

Visualisation of a door being opened with Lerp

Lerp can be used to move an object between two states, such as the open and closed positions of a door.

This creates a linear movement from one position to another, however, it's also possible to smooth the Lerp's movement using the Smooth Step function.

This works by modifying the t value with the Smooth Step function before passing it into Lerp.

Like this:

          float t = Mathf.SmoothStep(0, 1, timeElapsed / moveDuration);        

This will ease the movement of the object at the beginning and end of the Lerp.

How to move an object to a position using animation

Depending on how you'd like to control the movement of objects in your game, it can sometimes be easier to simply animate the object you'd like to move.

This typically involves modifying properties of an object and storing them in Keyframes, which are then smoothly animated between over time.

For example, you could use animation to move an object between two points, such as a floating platform.

This would need three Keyframes to work, one for the starting position, another for the platform's end position and a final Keyframe to return the platform to its original position, looping the animation.

To animate an object you'll need to add an Animator and an Animation Clip, which you can do from the Animation window.

Add an Animator dialogue in Unity

Next, select a position on the Animation Timeline, this will be the position of the 2nd Keyframe, when the platform is furthest away..

Animation Timeline selection in Unity

Assuming that, at zero seconds, the platform is at its starting position, this will be the halfway point of the animation which, in this case, is 5 seconds.

Next, enable Keyframe Recording by clicking the record button:

Enable keyframe recording

While Keyframe Recording is enabled, any changes you make to an object will be saved to a Keyframe at that timeline position, such as changing its position:

Screenshot - Recording transform changes to Keyframe

Create a final Keyframe at ten seconds, setting the object's position to its original values to complete the animation (remembering to disable Keyframe Recording when you're finished).

Animation Timeline selection in Unity with Keyframe Recording enabled

The end result is a platform that moves smoothly, and automatically, between its Keyframe positions.

These methods work well for creating controlled precise movements in Unity.

But, what if you don't want to control an object's movement precisely?

What if you'd rather push, pull or throw an object, to create movement using physical forces instead?

How to move an object using physics

Most rendered objects in Unity have a Collider component attached to them, which gives them a physical presence in the world.

However, an object with only a Collider is considered to be static, meaning that, generally speaking, it's not supposed to move.

To actually move an object under physics simulation, you'll also need to add a Rigidbody component to it, which allows it to move, and be moved, by physical forces, such as gravity.

You can also move a physics object by applying force to its Rigidbody using the Add Force function.

Like this:

          Rigidbody.AddForce(Vector3 force);        

This works in a similar way to the Translate function except that the vector you pass in is a physical force, not a movement amount.

How much the object moves as a result will depend on physical properties such as mass, drag and gravity.

There are two main ways to apply physical force on an object.

You can either apply force continuously, building momentum and speed over time, or all at once in an impulse, like hitting an object to move it.

By default, the Add Force function applies a continuous force, like a thruster gradually lifting a rocket.

Like this:

          public Rigidbody rb; public float forceAmount = 10; void FixedUpdate() {     rb.AddForce(Vector3.up * forceAmount); }        

Notice that I've used Fixed Update, and not Update, to apply the force to the object.

This is because Fixed Update is called in sync with the physics system, which runs at a different frequency to Update, which is often faster and can vary from frame to frame.

Doing it this way means that the application of force is in sync with the physics system that it affects.

Alternatively, you can also apply force in a single burst, using the Impulse Force Mode.

Like this:

          public Rigidbody rb; public float forceAmount = 10; void Start() {     rb.AddForce(Vector3.up * forceAmount, ForceMode.Impulse); }        

This will apply an amount of force to an object all at once:

Visualisation of the Add Force function in Unity

Add Force can be used to push objects gradually, or apply force in a single hit, like this.

Which can be useful for faster, more explosive movements, such as making an object jump.

  • How to jump in Unity (with or without physics)

Now it's your turn

How are you moving objects in your game?

Are you moving them using their Transform components?

Or are you moving them with physics?

And what have you learned about moving objects in Unity that you know others will find helpful?

Whatever it is, let me know by leaving a comment.

Get Game Development Tips, Straight to Your inbox

Get helpful tips & tricks and master game development basics the easy way, with deep-dive tutorials and guides.

My favourite time-saving Unity assets

Rewired (the best input management system)

Rewired is an input management asset that extends Unity's default input system, the Input Manager, adding much needed improvements and support for modern devices. Put simply, it's much more advanced than the default Input Manager and more reliable than Unity's new Input System. When I tested both systems, I found Rewired to be surprisingly easy to use and fully featured, so I can understand why everyone loves it.

DOTween Pro (should be built into Unity)

An asset so useful, it should already be built into Unity. Except it's not. DOTween Pro is an animation and timing tool that allows you to animate anything in Unity. You can move, fade, scale, rotate without writing Coroutines or Lerp functions.

Easy Save (there's no reason not to use it)

Easy Save makes managing game saves and file serialization extremely easy in Unity. So much so that, for the time it would take to build a save system, vs the cost of buying Easy Save, I don't recommend making your own save system since Easy Save already exists.

blackwoodmosencestiss.blogspot.com

Source: https://gamedevbeginner.com/how-to-move-objects-in-unity/