In the majority of games, one of the primary actions is moving objects. Whether it involves maneuvering our player character, controlling the enemy, or manipulating the environment, the movement mechanics play a crucial role in shaping the gameplay experience. However, it’s worth noting that certain games centered around user interfaces may not emphasize object movement as prominently. In this tutorial, we will look into the following topics to enhance our understanding.
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. If you are building a first-person shooter game then you should use the character controller for player movement.
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 code below, we are using a simple timer to wait for some time before changing position.
All the movement script are inside the Update method as we need to continuously move till the destination is reached.
Create a new script and copy the following code to the object you want to move. Remember to keep the script name and the class name same.
Sample unity 3D movement script
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
Slowly move to the new position in steps
To move an object slowly to a new position you can either use Unity lerp or use Unity Vector3.moveTowards. We are using steps of 0.1 in the sample code below.
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
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) to the current position. 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.deltaTime
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 get the keyboard’s arrow keys using Input.Getaxis
and then add the value to the transform of the object.
The Horizontal axis takes the left and right arrows and the 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. We are also using a speed variable to control the speed of player movement. You will need to do some trial and error until you get the right movement speed.
Just create a new script by name “move_player” and add the below line of code to it.
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
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.
- Get mouse position.
- Covert mouse position to world space.
- Move your object to the position.
Sample code
using UnityEngine;
public 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.
In this article, we saw the basics of 3D movement using script in Unity. The actual movement of the player will depend on the type of game you are making. There are different types of movement too. For example, if you are using a Rigidbody Component for physics and you need to detect collision then it’s a good idea to apply force to move the player object and not use transform.