How to move objects in unity (3 approaches with examples)

In unity by john FrenchOctober 14, 20216 Comments


Moving an object in Unity deserve to be really straightforward.

You are watching: Unity move object to position over time

It typically involves editing and enhancing the properties of an object’s Transform component, i beg your pardon is provided to control a game object’s scale, rotation and position in the world.

However, there are many different means you have the right to do that…

And when most approaches of moving an object in unity involve modifying the object’s transform in some way, the exact an approach you usage will vary depending on the kind of motion you desire to create.

But don’t worry, due to the fact that in this article, you’ll find out how and when to use each that the different methods because that moving an item in Unity, so that you have the right to use the one that’s best for her project.

Here’s what you’ll uncover on this page:

How come move an item in UnityHow come move an item to a position in Unity

Let’s acquire started.

How to move things in Unity

The most straightforward an approach of changing an object’s place in unity is to collection it directly, which will certainly instantly move it come a brand-new vector 3 place in the world.

This functions by setup the Position residential property of one object’s Transform component to a brand-new position.

Like this:

// Moves things to the set positiontransform.position = brand-new Vector3(10, 0, 5);Or friend can add a vector come an object’s position, to relocate it by a set amount in a specific direction.

Like this:

// Moves an item up 2 unitstransform.position += new Vector3(0, 2, 0);Which looks prefer this:


*
*
*

You can create camera relative movement by making use of the camera’s forward direction in location of the object’s.


By knowledge the family member direction of one object, it’s feasible to create any kind of movement regulate that friend want.

However, a many the time, you might want come move an item in a different way, that’s not directly regulated by the player.

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

How come move things to a position in Unity

Generally speaking, there room two various ways come move an item into a particular position.

By speed, whereby the thing moves in the direction of a target in ~ a certain speed,By time, wherein the movement in between the two points takes a particular amount the time to complete.

The method you usage will count on how you desire to control the object’s movement, by time or through speed.

Here’s just how both approaches work…

How come move things to a position at a collection speed (using move Towards)

It’s feasible to move an object towards one more object or a details position in the scene using the Move Towards function.

Move towards is a function of the Vector 3 class that will certainly modify a Vector 3 worth to relocate towards a target in ~ a set speed without overshooting.

Which works an excellent for moving an object towards a position in the human being at a speed you deserve to control.

Like this:

public Vector3 targetPosition;public float speed=10;void Update() transform.position = Vector3.MoveTowards(transform.position, targetPosition, rate * Time.deltaTime);The motion can also be smoothed, by utilizing the Smooth Damp function, i m sorry eases the motion as it starts and also ends.

Like this:

public Vector3 targetPosition;public to rise smoothTime = 0.5f; publicly float speed = 10;Vector3 velocity;void Update() transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime, speed);This functions by setup the place of the object to the Vector 3 result of the Smooth wet function, passing in a target, the current position and a reference Vector 3 value, i beg your pardon the duty uses to process the velocity the the object between frames.

This can be advantageous for smoothed consistent movement, whereby the target place may readjust from one moment to the following such as when complying with the player through a camera.

Such together in this instance of a smooth camera follow manuscript using Smooth Damp:

public transform player;public rise 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 also turn to challenge them, while keeping at a height of 3 units and trying to stay at a street of 5 systems away.

Move in the direction of works great as a means to move things towards a position dynamically.

This is due to the fact that the motion is speed-based, and also can be managed even if the target position moves or is unknown.

However, over there are many times wherein you might simply desire to move an item to a recognized position over a set duration of time.

Such as relocating a platform, or opening a door.

So just how so you develop time-based activity in Unity?

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

Lerp, or linear Interpolation, is supplied to uncover a value in between a minimum and maximum based on a place value, ‘t’, i beg your pardon is a float between zero and one.

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

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

float lerpedValue = Mathf.Lerp(float minValue, to rise maxValue, float t);Typically, Lerp is supplied to readjust a value over a period of time, through incrementing t every structure to return a brand-new value ~ above the scale.

This can be used to adjust a colour, fade one audio source or move things between 2 points.

It works by pass in the amount of time that has elapsed throughout the Lerp, split by the full duration. This returns a 0-1 float that have the right to be offered for the t value, and enables you to regulate the length of the Lerp motion by simply choosing how lengthy you’d favor it come take.

Like this:

float timeElapsed;float lerpDuration = 3;float lerpedValue;void Update(){ if (timeElapsed Vector3.Lerp functions in the same means except that, instead of returning a float, it return a allude in the world between two others, based on the t value.

This can be beneficial for moving things between two various positions, such together a door with an open and also closed state.

See more: Happy First Day Of Spring Pictures, Images And Stock Photos

Like this:

public rise openHeight = 4.5f;public float duration = 1;bool doorOpen;Vector3 closePosition;void Start() // set the very first position of the door as it"s close up door 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){ to rise timeElapsed = 0; Vector3 startPosition = transform.position; if (timeElapsed In this example, I’ve used a coroutine to move the door thing up by a collection distance native the door’s starting position, anytime the run Door duty is called.