Vector2 / Vector3 Operations
Basic operations for Vector2 (2D coordinates and directions) and Vector3 (3D coordinates and directions) in Unity, including distance calculation, interpolation, and normalization.
Syntax
using UnityEngine; // Create a Vector2 (2D). Vector2 v2 = new Vector2(float x, float y); // Create a Vector3 (3D). Vector3 v3 = new Vector3(float x, float y, float z); // Returns the distance between two points. float distance = Vector3.Distance(Vector3 a, Vector3 b); // Normalizes a vector (returns a vector with a length of 1). Vector3 normalized = Vector3.Normalize(Vector3 value); Vector3 normalized = v3.normalized; // Also available as a property. // Interpolates from a to b by t (0 to 1). Vector3 interpolated = Vector3.Lerp(Vector3 a, Vector3 b, float t); // Returns the length (magnitude) of a vector. float length = v3.magnitude;
Method List
| Method / Property | Description |
|---|---|
| Vector3.Distance(a, b) | Returns the Euclidean distance between points a and b. |
| Vector3.Lerp(a, b, t) | Returns a vector linearly interpolated from a to b by t (0 to 1). |
| Vector3.Normalize(v) | Returns a vector with the same direction as v but with a length of 1. |
| v.normalized | A property that returns the normalized version of vector v (does not modify the original vector). |
| v.magnitude | Returns the length (magnitude) of the vector. Slower than sqrMagnitude due to the square root calculation. |
| v.sqrMagnitude | Returns the squared length of the vector. Faster than magnitude when you only need to compare sizes. |
| Vector3.Dot(a, b) | Returns the dot product of a and b. Useful for determining the angular relationship between two vectors. |
| Vector3.Cross(a, b) | Returns the cross product of a and b (a vector perpendicular to both). |
| Vector3.zero / one / up / right / forward | Commonly used vector constants. |
Sample Code
using UnityEngine;
public class VectorSample : MonoBehaviour
{
[SerializeField] private Transform target;
[SerializeField] private float moveSpeed = 3f;
[SerializeField] private float attackRange = 5f;
void Start()
{
// Basic Vector3 operations.
Vector3 posA = new Vector3(0f, 0f, 0f);
Vector3 posB = new Vector3(3f, 4f, 0f);
// Calculate the distance between two points.
float distance = Vector3.Distance(posA, posB);
Debug.Log($"Distance: {distance}"); // 5
// Get the magnitude of a vector.
Vector3 moveDir = posB - posA;
Debug.Log($"magnitude: {moveDir.magnitude}"); // 5
// Normalize (convert to length 1).
Vector3 unitVector = moveDir.normalized;
Debug.Log($"normalized: {unitVector}"); // (0.6, 0.8, 0.0)
// Use the dot product to check if the target is in front.
Vector3 myForward = transform.forward;
Vector3 toTarget = (target.position - transform.position).normalized;
float dot = Vector3.Dot(myForward, toTarget);
bool isInFront = dot > 0f;
Debug.Log($"Target is in front: {isInFront}");
}
void Update()
{
if (target == null) return;
// Check if the target is within attack range using sqrMagnitude for efficiency.
Vector3 diff = target.position - transform.position;
if (diff.sqrMagnitude < attackRange * attackRange)
{
Debug.Log("Target is within attack range!");
}
// Smoothly move toward the target using Vector3.Lerp().
transform.position = Vector3.Lerp(
transform.position,
target.position,
moveSpeed * Time.deltaTime
);
}
}
Notes
When comparing distances (e.g., "is the target within 5 meters?"), comparing sqrMagnitude against the squared distance is faster than using Distance(). magnitude performs a square root calculation (Sqrt) internally, so calling it every frame inside Update() can add unnecessary overhead.
Vector2 and Vector3 can be implicitly converted to each other (the z component is treated as 0). For Unity's math functions in general, see Mathf. For time-based animations, consider using Coroutine / IEnumerator.
If you find any errors or copyright issues, please contact us.