How to move an Object in Unity?

Moving an object is what we do in most of the games. Either we move our player or the enemy or the environment to create the gameplay experience. In this tutorial we will learn the following

How to move an object to a position

There are multiple ways to move an object from one position to another. We will see all of them one by one.

Instantly changing position

You can change the position of a gameobject instantly using the Unity transform function. All you need to do is to set the new position as the object’s transform. In the below code, i am using a simple Unity timer to wait for some time before changing position.

Attach the below code to the object you want to move.

Sample code

using UnityEngine;
public class move_player : MonoBehaviour
{
    Vector3 newpos= new Vector3(-4.0f,0.5f,-4.0f);
    float timer=2;
    // Update is called once per frame
    void Update()
    {
        timer-=Time.deltaTime;
        if(timer<0)
        {
            transform.position=newpos;
        }
        
    }
}

Output

Gif output. Moving object instantly from one position to another.

Slowly move to the new position in steps

To move an object slowly to a new position you can either use Unity lerp or using vector3.moveTowards. We are using steps of 0.1 in the below sample code.

Sample code

using UnityEngine;
public class move_player : MonoBehaviour
{
    Vector3 newpos= new Vector3(-4.0f,0.5f,-4.0f);
    // Update is called once per frame
    void Update()
    {
       transform.position=Vector3.MoveTowards(transform.position,newpos,0.1f);
        
    }
}

Output

object moving slowly form one position to another

Slowly moving to new position over time

You can use Vector3.SmoothDamp to move an object overtime. You need to provide the velocity as the third variable using the ref keyword as shown in the example code below.

Sample code

using UnityEngine;
public class move_player : MonoBehaviour
{
    Vector3 newpos= new Vector3(-4.0f,0.5f,-4.0f);
    Vector3 velocity=Vector3.zero;
    // Update is called once per frame
    void Update()
    {
       transform.position=Vector3.SmoothDamp(transform.position,newpos,ref velocity,2.0f);
        
    }
}

Output

How to move an object in a direction

Direction is generally defined by a unit vector. If you want to move your object in the direction of another object then you can normalize the position of the object to get the direction.

You can move up, down, forward and backward by adding the transform.up(down, forward and backward respectively). You can also use vector3.up for this purpose. The sample code below uses transform.forward to move the cube in the forward direction.

Always remember to multiple the direction vector with Time.delta time to move if gradually.

Sample code

using UnityEngine;
public class move_player : MonoBehaviour
{
    Vector3 newpos= new Vector3(-4.0f,0.5f,-4.0f);
    Vector3 velocity=Vector3.zero;
    // Update is called once per frame
    void Update()
    {
       transform.position+=transform.forward*Time.deltaTime;
        
    }
}

Output

How to move an object with keyboard

To move an object in Unity you need to take the keyboard input using Input.Getaxis and then add the value to the transform of the object.

The Horizontal axis takes the left and right arrows and hte vertical axis takes the up and down arrows.

Unity by default operates in the xz plane so we will add the input to the x and z axis. In case of 2D, you can add the input to the x and y axis.

Sample code

using UnityEngine;
public class move_player : MonoBehaviour
{
    Vector3 pos;
    float speed=5f;
    void Start()
    {
        pos=transform.position;
    }
    // Update is called once per frame
    void Update()
    {
       float x_move=Input.GetAxis("Horizontal");
       float z_move=Input.GetAxis("Vertical"); 
       pos.x+=x_move*Time.deltaTime*speed;
       pos.z+=z_move*Time.deltaTime*speed;
       transform.position=pos;
        
    }
}

Output

object moving using keyboard input in unity.

How to move an object to mouse click position

This process is a little complex compared to the other movements. It involves the following three steps.

  1. Get mouse position.
  2. Covert mouse position to world space.
  3. Move your object to the position.

Sample code

c class move_player : MonoBehaviour
{
    Vector3 mouse_world_pos;
    Vector3 mouse_pos;
    
    // Update is called once per frame
    void Update()
    {
        if(Input.GetMouseButton(0))
        {
            mouse_pos=Input.mousePosition;
            mouse_pos.z=Camera.main.nearClipPlane;
            mouse_world_pos=Camera.main.ScreenToWorldPoint(mouse_pos);            
        }
        else
        {
            mouse_world_pos=transform.position;
        }
       transform.position=Vector3.MoveTowards(transform.position,mouse_world_pos,0.1f);
        
    }
}

If you want to move the object on a plane using mouse position then it’s better to use Unity Raycast to figure out the position on the plane and then move the object to that position.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.