Movement by Transform
The Transform
component is one of the most fundamental components in Unity and is found in virtually all game objects. It represents the position, rotation, and scale of an object in the 3D or 2D space of the game world. In essence, the Transform
component allows controlling the location and orientation of an object in the scene.
Here's a description of the key properties of the Transform
component:
Position: The
position
property defines the three-dimensional location of the object in the game world. It's a vector consisting of three values (X, Y, and Z) that represent the object's position in a coordinate system.Rotation: The
rotation
property controls the object's orientation in space. It's a quaternion that describes the object's rotation. In Unity, rotations are handled using quaternions to avoid gimbal lock problems and offer a more precise representation.Scale: The
localScale
property determines the object's size on each of the axes (X, Y, and Z). You can scale an object to make it larger or smaller relative to its original size.Parent and Child Relationships: Objects in Unity can be linked in an object hierarchy, where one object can be the parent of other objects. This is useful for creating related object systems, like 3D models with moving parts. The transformation of a child object is performed relative to its parent's transformation.
The Transform
component is used in conjunction with scripts in C# to perform actions, such as moving an object, rotating it, scaling it, or performing any necessary transformation in the game world. For example, to move an object, you can access the position
property of the Transform
component and change its values based on time and speed using Time.deltaTime
, as mentioned in the previous response.
Here's a simple example in C# that moves an object forward on each frame:

transform
is our component. And Translate
is a transform function that asks us for a vector as an argument. Do you get it? It's the same thing we've been doing in previous classes but with what Unity provides us.
Instead of manually putting 0,0,1
indicating that we want to move 0 on the X axis, 0 on Y, and 1 on Z. We can directly pass Vector3.forward

Now, the object's speed is out of control. We must change the code to fix it.

In Unity, the Time.deltaTime
variable represents the time in seconds that has elapsed since the last rendered frame. This variable is essential for performing time-dependent calculations, such as smooth object movement or physics simulation.
The equation underlying the Time.deltaTime
calculation is as follows:
Time.deltaTime = (Current frame time - Previous frame time)
(If we don't multiply the movement vector * Time.deltatime what happens is that we are moving the object X units per frame. Regardless of time. That is, on a computer where the game runs at 120 frames per second, the object moves at 120 units per second. And on a computer where it runs at 50 frames per second it will move at 50 units per second. This is not what we want.
Unity automatically calculates this time difference on each frame and stores it in Time.deltaTime
so you can use it in your code.
Here's an example of how Time.deltaTime
can be used to move an object at a constant speed based on time:
So the code would look like this:
using UnityEngine;
public class ObjectMovement : MonoBehaviour
{
public float speed = 5.0f; // Object speed in units per second
void Update()
{
// Move the object forward at a constant speed
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
}
Rotational Movement
Relative movement involves changing the position or rotation of an object relative to its current state. You can do this by directly adjusting the position
or rotation
properties of the Transform
component. Here's an example of a script that rotates an object around its local Y axis:
using UnityEngine;
public class RelativeRotation : MonoBehaviour
{
public float rotationSpeed = 45.0f; // Degrees per second
void Update()
{
// Relative rotation to local Y axis
transform.Rotate(Vector3.up * rotationSpeed * Time.deltaTime);
}
}
Double Rotation Movement
You can perform more complex rotations by combining multiple rotation axes. Here's an example that rotates an object around its local X axis and then around its local Y axis:
using UnityEngine;
public class ComplexRotation : MonoBehaviour
{
public float rotationSpeedX = 30.0f; // Degrees per second
public float rotationSpeedY = 60.0f; // Degrees per second
void Update()
{
// Complex rotation
transform.Rotate(Vector3.right * rotationSpeedX * Time.deltaTime);
transform.Rotate(Vector3.up * rotationSpeedY * Time.deltaTime);
}
}
Movement Following a Path
You can make an object follow a specific path or trajectory using the Transform
component. This involves gradually adjusting the object's position based on time. Here's a basic example of how to make an object move following a path:
using UnityEngine;
public class PathFollowingMovement : MonoBehaviour
{
public Transform[] pathPoints; // Array of path points
public float speed = 2.0f; // Movement speed
private int currentPoint = 0;
void Update()
{
if (currentPoint < pathPoints.Length)
{
// Calculate the direction to the next path point
Vector3 direction = (pathPoints[currentPoint].position - transform.position).normalized;
// Move the object toward the next path point
transform.Translate(direction * speed * Time.deltaTime);
// If the object is close enough to the point, move to the next one
if (Vector3.Distance(transform.position, pathPoints[currentPoint].position) < 0.1f)
{
currentPoint++;
}
}
}
}
Sinusoidal Movement in a Straight Line:
This example will make an object move forward and backward following a sinusoidal trajectory in a straight line. We'll use the sine function to adjust the object's position based on time.
using UnityEngine;
public class SinusoidalMovement : MonoBehaviour
{
public float amplitude = 2.0f; // Movement amplitude
public float frequency = 2.0f; // Movement frequency (oscillations per second)
public float speed = 2.0f; // Movement speed
private Vector3 initialPosition;
void Start()
{
initialPosition = transform.position;
}
void Update()
{
// Calculate the new position based on time
float horizontalDisplacement = amplitude * Mathf.Sin(Time.time * frequency);
Vector3 newPosition = initialPosition + Vector3.right * horizontalDisplacement;
// Move the object to the new position
transform.position = Vector3.Lerp(transform.position, newPosition, speed * Time.deltaTime);
}
}
Oscillating Movement in One Direction:
In this example, we'll make an object oscillate from side to side along the X axis using the cosine function. The amplitude controls how much it moves from side to side.
using UnityEngine;
public class OscillatingMovement : MonoBehaviour
{
public float amplitude = 2.0f; // Movement amplitude
public float speed = 2.0f; // Movement speed
private Vector3 initialPosition;
void Start()
{
initialPosition = transform.position;
}
void Update()
{
// Calculate the displacement on the X axis based on time
float displacementX = amplitude * Mathf.Cos(Time.time * speed);
// Update the object's position
transform.position = initialPosition + Vector3.right * displacementX;
}
}
Both examples use sine and cosine functions to create sinusoidal movements in Unity. You can adjust parameters like amplitude, frequency, and speed to achieve the desired behavior. These movements add a more dynamic and visually appealing aspect to your objects in the game.
These are just basic examples of movements you can achieve using the Transform
component in Unity. Depending on your needs, you can combine these concepts to create more sophisticated movements and animations in your games and applications.
Geogebra
For the following exercise I recommend you go to https://www.geogebra.org/calculator. There you can write your own functions and understand how the operations we can perform with cosine and sine relate.
If we write a(x)=sin(x)
we will see how our graph is formed.

Now, what happens if we multiply X by 4?

The frequency increases. How do you think this would affect our object that we move in Unity?
And if we multiply outside the parentheses?

The amplitude increases! And if instead of multiplying we add? Try it!
Last updated